Skip to content

Instantly share code, notes, and snippets.

@justgrimes
Created April 17, 2012 17:01
Show Gist options
  • Save justgrimes/2407498 to your computer and use it in GitHub Desktop.
Save justgrimes/2407498 to your computer and use it in GitHub Desktop.
sql snippet for calculating distance for latitude, longtitude
SELECT latitude,longitude, (3963.191 * ACOS(
(SIN(PI()* 40.7383040 /180)*SIN(PI()*latitude/180)) +
(COS(PI()* 40.7383040 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -73.99319 /180))
)) AS distance
FROM TABLE
WHERE 1=1
AND 3963.191 * ACOS( (SIN(PI()* 40.7383040 /180)*SIN(PI() * latitude/180)) +
(COS(PI()* 40.7383040 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -73.99319 /180))
) <= 1.5
ORDER BY 3963.191 * ACOS(
(SIN(PI()* 40.7383040 /180)*SIN(PI()*latitude/180)) +
(COS(PI()* 40.7383040 /180)*cos(PI()*latitude/180)*COS(PI() * longitude/180-PI()* -73.99319 /180))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment