I\'ve centroids for customer A and customer B. Now I need to calculate distance between customer A and B into miles using centroids. How can I do it in Oracle?
Right no
If you have Cartesian coordinates then the approximate distance if given by the Pythagorean theorem as already provided by Matthew.
For Lat/Lon values you should use the Oracle build in SDO_GEOM.SDO_DISTANCE function, if available.
If your Oracle DB does not have Oracle Spatial installed (it cost extra) then you can use the Haversine formula to get approximate distance like this:
CREATE OR REPLACE FUNCTION p2p_distance(
p_latitude1 NUMBER,
p_longitude1 NUMBER,
p_latitude2 NUMBER,
p_longitude2 NUMBER)
RETURN NUMBER DETERMINISTIC IS
earth_radius NUMBER := 6371;
pi NUMBER := ACOS(-1)/180;
lat_delta NUMBER;
lon_delta NUMBER;
arc NUMBER;
BEGIN
lat_delta := (p_latitude2-p_latitude1)*pi;
lon_delta := (p_longitude2-p_longitude1)*pi;
arc := SIN(lat_delta/2) * SIN(lat_delta/2) + SIN(lon_delta/2) * SIN(lon_delta/2) * COS(p_latitude1*pi) * COS(p_latitude2*pi);
return earth_radius * 2 * atan2(sqrt(arc), sqrt(1-arc));
END;
Result is given in kilometers, if you like to get miles then replace earth_radius
by according value in miles.
credits: https://connor-mcdonald.com/2017/01/17/haversine-plsql/