How to find the nearest numeric values (also in decimal places) in SQL same as LIKE function

42 views Asked by At

I want to find the nearest coordinates to validate the place name as the format type is FLOAT, then I cannot use LIKE function as it is only applicable for STRING format.

situation example; my reference is (latitude, longitude) that have short decimal places such as (67.1, -69.2) where in the list the coordinates is at five decimal places.

so I need to use my reference coordinates to find any other coordinates that start with same reference coordinates.

I already use LIKE function but cannot use it as it is allowable for STRING only.

1

There are 1 answers

1
SebCza On

sorry if i misunderstood you but you could figure it out by simple math:

SELECT * FROM table_lat_len_tst;

enter image description here

SELECT 
    t2.id
    , t2.lat
    , t2.lon
    , ABS(t1.lat - t2.lat) diff_lat
    , ABS(t1.lon - t2.lon) diff_lon
    , ABS(t1.lat - t2.lat) + ABS(t1.lon - t2.lon) delta
FROM 
    table_lat_len_tst t1
        JOIN table_lat_len_tst t2 ON t2.id != t1.id
WHERE
    t1.id = 1 -- Neares to ID
ORDER BY
    delta
FETCH FIRST 1 ROWS ONLY;

enter image description here

Let me know if it was helpful.