TSQL - Distance in kilometers between 2 points, given the latitude and longitude

It's always good to know the distance between 2 points.

Here's the SQL to get you started - notice the @dtor constant - this will get you the return value in kilometers.

DECLARE @dtor float,       
        @lat1 decimal(19,6),
        @lon1 decimal(19,6),
        @lat2 decimal(19,6),
        @lon2 decimal(19,6)
set @dtor = 57.295800

set @lat1 = 44.11451
set @lon1 = -77.55602

set @lat2 = 44.12451
set @lon2 = -78.51602

--do the actual calculation

select  (6371 * acos(sin(@lat1/@dtor) * sin(@lat2/@dtor) + cos(@lat1/@dtor) * cos(@lat2/@dtor) * cos(@lon2/@dtor - @lon1/@dtor)))

 

More later - joel.

2 Comments

Comments have been disabled for this content.