October 15, 2015 at 7:53 am
DECLARE @Latitude NUMERIC(9, 6), @Longitude NUMERIC(9, 6)
DECLARE @MyLatitude NUMERIC(9, 6), @MyLongitude NUMERIC(9, 6)
Set @Latitude = 42.329596;
Set @Longitude = -83.709286;
Set @MyLatitude = 42.430883;
Set @MyLongitude = -82.923642;
Question: How do we calculate the distance in miles between the 2 points.
October 15, 2015 at 7:56 am
October 15, 2015 at 8:01 am
i have this saved in my snippets; does ~7 miles sound correct for what you posted?
Miles
6.99824265061246
DECLARE
@Latitude NUMERIC(9, 6),
@Longitude NUMERIC(9, 6),
@MyLatitude NUMERIC(9, 6),
@MyLongitude NUMERIC(9, 6)
Set @Latitude = 42.329596;
Set @Longitude = -83.709286;
Set @MyLatitude = 42.430883;
Set @MyLongitude = -82.923642;
Select
Avg(3958.75 * ACos(Sin(@Latitude/57.2958) *
Sin(@MyLatitude/57.2958) +
Cos(@Latitude/57.2958) *
Cos(@MyLatitude/57.2958) *
Cos(@MyLongitude/57.2958 - @MyLongitude/57.2958))) As Miles
Lowell
October 15, 2015 at 8:43 am
Thanks
But I found another way.... The answers that I am getting are different when I do your way vs this way. I think I will go with the clode below. But thanks
DECLARE @RangeDivisor FLOAT
SET @RangeDivisor = 1609.344;
DECLARE @MyLatitude NUMERIC(9, 6), @MyLongitude NUMERIC(9, 6)
Set @MyLatitude = 42.430883;
Set @MyLongitude = -82.923642;
create table #r ( LATx FLOAT, LONGTx FLOAT, GEO GEOGRAPHY );
INSERT INTO #r ( LATx, LONGTx ) VALUES ( 42.329596, -83.709286 );
UPDATE #r
Set GEO = GEOGRAPHY::STGeomFromText('Point(' + CAST([LATX] AS VARCHAR(32)) + ' ' + CAST(LONGTx AS VARCHAR(32)) + ')',4326);
Select CAST(GEO.STDistance(geography::Point(@MyLatitude, @MyLongitude, 4326))/@RangeDivisor AS DECIMAL(18,1)) [Distance]
FROM
#r;
October 15, 2015 at 9:09 am
Slight error in that previous code section...... This is correct
DECLARE @RangeDivisor FLOAT
SET @RangeDivisor = 1609.344;
DECLARE @MyLatitude NUMERIC(9, 6), @MyLongitude NUMERIC(9, 6)
Set @MyLatitude = 42.430883;
Set @MyLongitude = -82.923642;
create table #r ( LATx FLOAT, LONGTx FLOAT, GEO GEOGRAPHY );
INSERT INTO #r ( LATx, LONGTx ) VALUES ( 42.329596, -83.709286 );
UPDATE #r
Set GEO = GEOGRAPHY::STGeomFromText('Point(' + CAST(LONGTx AS VARCHAR(32)) + ' ' + CAST(LATx AS VARCHAR(32)) + ')',4326);
Select CAST(GEO.STDistance(geography::Point(@MyLatitude, @MyLongitude, 4326))/@RangeDivisor AS DECIMAL(18,1)) [Distance]
FROM
#r;
--NOTE: WHEN USING THE STGeomFromText you must past the LONGT as the first parameter.... That was the error
October 15, 2015 at 2:08 pm
mw112009 (10/15/2015)
Slight error in that previous code section...... This is correctDECLARE @RangeDivisor FLOAT
SET @RangeDivisor = 1609.344;
DECLARE @MyLatitude NUMERIC(9, 6), @MyLongitude NUMERIC(9, 6)
Set @MyLatitude = 42.430883;
Set @MyLongitude = -82.923642;
create table #r ( LATx FLOAT, LONGTx FLOAT, GEO GEOGRAPHY );
INSERT INTO #r ( LATx, LONGTx ) VALUES ( 42.329596, -83.709286 );
UPDATE #r
Set GEO = GEOGRAPHY::STGeomFromText('Point(' + CAST(LONGTx AS VARCHAR(32)) + ' ' + CAST(LATx AS VARCHAR(32)) + ')',4326);
Select CAST(GEO.STDistance(geography::Point(@MyLatitude, @MyLongitude, 4326))/@RangeDivisor AS DECIMAL(18,1)) [Distance]
FROM
#r;
--NOTE: WHEN USING THE STGeomFromText you must past the LONGT as the first parameter.... That was the error
So what did that come up with a distance as for the original problem?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply