How to get all other zip codes or (latitude and longitude) from the table next to a given zip code and a given radius in miles on sql server?

I have a table with latitude, longitude and zip codes. I want to select records that are next to a given zip code and radius in miles. for example, the user will enter postcode = 'NW44JL' and radius = 1 mile. I need to get all records from a table with a radius of 1 mile from NW44JL. Can anyone help me get this. Thanks

+4
source share
2 answers
Declare @radius int
--Radius for circle
set @radius=1

select distinct tb1.postcodes from table1 tb1
join table1 tb2 on (Power((tb1.latitude - tb2.latitude),2) + POWER((tb1.longitude - tb2.longitude),2)) < POWER(@radius,2)
where tb2.postcodes='NW44JL'

table1 is the name of your table

means latitude and longitude in miles. If you do not need to convert to miles

+1

Source: https://habr.com/ru/post/1541714/


All Articles