0 votes
1 view
in SQL by (20.3k points)

I have latitude and longitude and I want to pull the record from the database, which has nearest latitude and longitude by the distance, if that distance gets longer than the specified one, then don't retrieve it.

Table structure:

id

latitude

longitude

place name

city

country

state

zip

sealevel

1 Answer

0 votes
by (40.3k points)

Try the below code:

SELECT latitude, longitude, SQRT(

    POW(69.1 * (latitude - [startlat]), 2) +

    POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance

FROM TableName HAVING distance < 25 ORDER BY distance;

In the above code [starlat] and [startlng] is the position to start measuring the distance.

Related questions

0 votes
1 answer
asked Jul 23, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 24, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...