Write sql code to calculate distance between latitude and longitude coordinates
- User
Write sql code to calculate distance between latitude and longitude coordinates
- Assistant
The following SQL code uses the Haversine formula to calculate the distance between two sets of latitude and longitude coordinates in miles:
scssWITH locations AS ( SELECT lat1, lng1, lat2, lng2 FROM your_table_name ) SELECT lat1, lng1, lat2, lng2, 3959 * acos( cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(lng2) - radians(lng1)) + sin(radians(lat1)) * sin(radians(lat2)) ) AS distance FROM locationsNote that the earth's radius
3959is used in this formula and can be changed to6371for kilometers. - User
Write sql code to calculate distance in feet between latitude and longitude coordinates
- Assistant
The following SQL code uses the Haversine formula to calculate the distance between two sets of latitude and longitude coordinates in feet:
scssWITH locations AS ( SELECT lat1, lng1, lat2, lng2 FROM your_table_name ) SELECT lat1, lng1, lat2, lng2, 3959 * 5280 * acos( cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(lng2) - radians(lng1)) + sin(radians(lat1)) * sin(radians(lat2)) ) AS distance FROM locationsNote that the earth's radius
3959is used in this formula, multiplied by5280to convert miles to feet. This value can be changed to6371for kilometers.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.