Search code examples
oracle-databasegeometryoracle-spatial

Distance between two Point with Latitude & Longitude in oracle DB


I need to calculate Distance between two location through Pl SQL Query. I had tried some Qry but not getting it.

for this we are using Query in SQL is as follows

SELECT ROUND(geography::Point(cast('19.2806118' as float),cast('72.8757395' as float) , 
4326).STDistance(geography::Point(cast('19.4482006' as float),
cast('72.7912511' as float), 4326))/1000,1) DIST

Result : 20.6

To Calculate the same in ORACLE Db I have found a solution over internet which is as follows

select sdo_geom.sdo_distance(sdo_geometry(2001,4326,null,sdo_elem_info_array(1, 1, 1),
sdo_ordinate_array( 19.2806118,72.8757395)),sdo_geometry(2001,4326, null,sdo_elem_info_array(1, 1, 
1),sdo_ordinate_array( 19.4482006,72.7912511)),1,'unit=KM') distance_km from dual

Result : 10.9271..

Please suggest the method which will give me result in KM as I am getting MS SQL


Solution

  • Please suggest the method which will give me result in KM as I am getting MS SQL

    Swapping parameter order 19.2806118,72.8757395 to 72.8757395,19.2806118

    select sdo_geom.sdo_distance(
       sdo_geometry(2001,4326,null,sdo_elem_info_array(1, 1, 1),
                     sdo_ordinate_array(72.8757395,19.2806118)),
       sdo_geometry(2001,4326, null,sdo_elem_info_array(1, 1,1),
                     sdo_ordinate_array(72.7912511,19.4482006))
       ,1
       ,'unit=KM') distance_km 
    from dual;
    

    Yields exactly the same result as SQL Server: 20.5657053685075

    db<>fiddle demo Oracle
    db<>fiddle demo SQL Server

    Or by using sdo_point:

    SELECT sdo_geom.sdo_distance(
             sdo_geometry(2001, 4326,
                    sdo_point_type(72.8757395, 19.2806118, NULL), NULL, NULL),
              sdo_geometry(2001, 4326,
                   sdo_point_type(72.7912511, 19.4482006, NULL), NULL, NULL),
                   1, 'unit=KM') distance_in_m
    from DUAL;
    

    SQL Server: geography::Point ( Lat, Long, SRID )