SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Calculate Earth global distances

By Neil Jacobson,

I wrote this sp to be able to calculate the distance between two locations on the earth if the latitude and longitude is known for each.  Could be useful for GPS work or maps.

If I know the following city locations:
Sydney         151.2 E             33.87 S
Canberra       149.15 E             35.24 S

DECLARE @Dist decimal(28,20)
EXEC @RC = [dbo].[sp_EarthDistance] 149.15, -35.24, 151.2, -33.87, @Dist OUTPUT
Select @RC, 'Distance from Sydney and Canberra = ', @Dist

Result (kilometers):
0           Distance between Sydney and Canberra =  242.04260703665335000000

You can change the precision or distance units yourself.

Total article views: 729 | Views in the last 30 days: 5
Related Articles

Calculate Distance Between Two Points

This function can be use to calculate distance between two points.


Calculate distance between 2 coordinates/latlong

This function is used to get the distance between 2 coordinates/latlong in the different formats


TSQL Challenge 34-Search for two keywords within the maximum distance

This challenge is all about searching for two keywords in a string with a maximum distance of 'one w...


Sydney SQL Server Users Group Draws Big Crowd

On Tuesday, October 13, I spoke at the Sydney SQL Server Users Group, presenting on “How to Get th...


Calculate Distance in Miles from Latitude and Longitude

A fast and accurate way to calculate the distance in miles between two points, based on the latitude...