So I have a table with a bunch of different addresses in it. I need a proc that will select the addresses in that table that are within a specified distance in miles from the passed in lat/long values.
So example of my table:
- messageId
- lat (float)
- long (float)
Proc is passing in another lat/long pair (both float
s as well) as well as an int
(miles)
I found this http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81360 to calculate the actual formula but I can't figure out to modify it in proc form to be able to go through an entire list of addresses and only give me the Id
's of the addresses that are <= the miles (that I pass in), from the lat/long I pass in.
Can I get any help here?
Thanks!
you can just use the function directly in the SP... I was thinking:
I actually wrote a short blog post a while back for exactly this purpose. Basically, the query I have is:
Give it the center latitude, the center longitude, and the search radius and you should be good. (The parameters for the equatorial and polar radii of the Earth can be hard-coded, naturally.)
All that math is supposed to account for the curvature of the earth, the bulging at the equator, etc.
SQL Server 2008
Using the spacial function
STDistance
return distance in metersgeography::Point(@lat1, @lon1, 4326).STDistance(geography::Point(@lat2, @lon2, 4326))