Uses the haversine formula to calculate distance between 2 long / lat points. This can be used with a zip code / lat and long table that are available from the census bureau or post office.
The @R can be tweaked dependent on what gives you the best result (its the radius of the earth, fluctuates based on your location, its in miles in my function, change it to meters for metric calculations). @DtoR is the number used in converting degrees to radians. This takes lat and long as degrees.
Info on haversine: http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1 or just search on google.
This could be used as follows:
select * from people p inner join zipcodes z on z.zipcode = p.zipcode where dbo.udf_haversine(z.lat, z.long, @inputLat, @inputLong) < 50
(People within 50 miles of a specific lat / long point)
A Normalization Primer
For most DBAs, normalization is an understood concept, a bread and butter bit of knowledge. However, it is not at all unusual to review a database design by a development group for an OLTP (OnLine Transaction Processing) environment and find that the schema chosen is anything but properly normalized. This article by Brian Kelley will give you the core knowledge to data model.
2003-01-13
18,597 reads