Skip to content

Instantly share code, notes, and snippets.

@natxet
Forked from Usse/distance.sql
Last active November 3, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natxet/ca1d3b6cbcd241d5d6fb to your computer and use it in GitHub Desktop.
Save natxet/ca1d3b6cbcd241d5d6fb to your computer and use it in GitHub Desktop.
DELIMITER //
CREATE FUNCTION `lat_lng_distance` (lat1 FLOAT, lng1 FLOAT, lat2 FLOAT, lng2 FLOAT)
RETURNS FLOAT
DETERMINISTIC
BEGIN
RETURN 6371 * 2 * ASIN(SQRT(
POWER(SIN((lat1 - abs(lat2)) * pi()/180 / 2),
2) + COS(lat1 * pi()/180 ) * COS(abs(lat2) *
pi()/180) * POWER(SIN((lng1 - lng2) *
pi()/180 / 2), 2) ));
END//
DELIMITER ;
--Returns the distance in kilometers, assuming a earth radius of 6,371 km.
@vaxhax
Copy link

vaxhax commented Jun 13, 2016

Thanks for the fork. The DELIMITER tags made the difference for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment