troubleshooting Question

Convert Haversine formula from PHP for use with SQL

Avatar of sluggo050499
sluggo050499 asked on
Microsoft SQL Server
3 Comments1 Solution1081 ViewsLast Modified:
The Haversine formula calculates the distance between 2 locations utilizing lat/long.  I have a PHP script that works to get this.  It uses lat/long in degrees and returns the distance in miles.
-----------------------------------------------------
# Where:
#  $l1 ==> latitude1
#  $o1 ==> longitude1
#  $l2 ==> latitude2
#  $o2 ==> longitude2
function haversine ($l1, $o1, $l2, $o2)
{
     $l1 = deg2rad ($l1);
     $sinl1 = sin ($l1);
     $l2 = deg2rad ($l2);
     $o1 = deg2rad ($o1);
     $o2 = deg2rad ($o2);
                   
     return (7926 - 26 * $sinl1) * asin (min (1, 0.707106781186548 * sqrt ((1 - (sin ($l2) * $sinl1) - cos ($l1) * cos ($l2) * cos ($o2 - $o1)))));
}
------------------------------------------------

I have a database with id, latitude and longitude.  I want to enter the miles and the second lat/long and have the query return all of the id's that have that distance or less.
ASKER CERTIFIED SOLUTION
Brendt Hess
Senior DBA

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros