To calculate the distance in meters between two points using their latitude and longitude in Oracle, you can use the Haversine formula with Earth’s radius set to 6,371,000 meters (6,371 km).
1. Using the Haversine Formula in SQL
Parameters:
:lat1, :lon1
→ Latitude and Longitude of Point 1.:lat2, :lon2
→ Latitude and Longitude of Point 2.6371000
→ Earth's radius in meters.RADIANS(x)
→ Converts degrees to radians.
2. Example Calculation
Find the distance between:
- New Delhi (Lat:
28.6139
, Lon:77.2090
) - Mumbai (Lat:
19.0760
, Lon:72.8777
)
Query:
Output:
3. Creating a PL/SQL Function for Reusability
If you frequently need to calculate distances, create a PL/SQL function:
Usage:
Key Points
- Use
6371000
for meters,6371
for kilometers,3958.8
for miles. - The function
ACOS(COS() * COS() * COS() + SIN() * SIN())
applies the Haversine formula. RADIANS(x)
converts degrees to radians for accurate calculations.
Tags:
Oracle