Solved

Distance Between 2 Points of Latitude and Longitude

Posted on 2013-01-24
2
1,008 Views
Last Modified: 2013-01-24
How can I measure the distance between 2 customers. For example, One customer is at Point A with Latitude  of 42.91 and Longitude of -77.70 and the other is at Point B with Latitude of 42.09 and Longitude of -80.12.

I am using SqlServer 2012.


The data resides in my table in the following manner:

Customer    Lat (float)    Long (float)
1                   42.91             -77.70
2                   42.09              -80.12
0
Comment
Question by:nirajkrishna
2 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38817209
DECLARE @a geography = geography::Point(42.91, -77.70, 4326)
DECLARE @b geography = geography::Point(42.09, -80.12, 4326)

SELECT @a.STDistance(@b)



SRID of 4326 will cause the results to be returned in meters

218773.342240962



or, in the form of a query...


select geography::Point(a.lat, a.long, 4326).STDistance(geography::Point(b.lat,b.long,4326))  from yourtable a, yourtable b
where a.customer = 1
and b.customer = 2;
0
 

Author Closing Comment

by:nirajkrishna
ID: 38817264
Awesome answer! And very nice job including the TSql. I am a bit of a newb and your answer was perfect!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now