Home All Groups Group Topic Archive Search About

SQL distance between Latitude and Longitude?

Author
28 Jul 2006 8:50 PM
John Baima
Does anyone have a script for calculating the distance between two
different points expressed in Latitude and Longitude? I thought that
we had that in our database, but we were not actually storing Latitude
and Longitude but some calculated values and I would like to change it
to just straight Latitude and Longitude. Thanks.

-John

Author
28 Jul 2006 9:32 PM
Steve Kass
John,

Here is one solution.  You may have to adjust it depending on the units
you are using.  I think this version gives the answer in kilometers.

create function uf_Distance (
  @FromLat float, @FromLong float, @ToLat float, @ToLong float
) returns float as begin

declare @X float
SET @X =
   Sin(Radians(@FromLat))
* Sin(Radians(@ToLat))
+ Cos(Radians(@FromLat))
* Cos(Radians(@ToLat))
* Cos(Radians(@ToLong)-Radians(@FromLong))

SET @X = Acos(@X)
RETURN 1.852 * 60.0 * Degrees(@X)

end

go
select dbo.uf_Distance (41.63,-87.73,41.7,-88.07)
go

-- Steve Kass
-- Drew University
-- http://www.stevekass.com

John Baima wrote:

Show quote
>Does anyone have a script for calculating the distance between two
>different points expressed in Latitude and Longitude? I thought that
>we had that in our database, but we were not actually storing Latitude
>and Longitude but some calculated values and I would like to change it
>to just straight Latitude and Longitude. Thanks.
>
>-John
>

>

AddThis Social Bookmark Button