A query to return rows located at a specific geographic distance from a given row (using SQL Server 2008)

I have a table with several records, each of which contains a field called "coords". This field has been updated with a geographical point.

UPDATE testing SET [coords] = geography::Point(52.029736, -113.973541, 4326)
WHERE id=2"

What do I need to do ... when the user is logged in, they have a record belonging to them, because in this example their record identifier # 1 is indicated. They need to visit a page that shows all the other records, the โ€œcoordinatesโ€ fields at a certain distance.

This is the best I have come up with;

Fourth, I can find a beginning coordinated with this statement;

SELECT coords FROM testing WHERE id=1

This gives me the original coordinate as coords.Lat and coords.Long

Then I would like to find them nearby, so I have this:

SELECT * FROM testing WHERE coords.STDistance() <=(20 * 1609.344)

, , .

, - coords.Lat/coords.Long STDistance? , , .

FYI, SQL-, "", select * from table where record = 1 .

+3
3

?

SELECT
    [near].* 
FROM 
    testing [near]
INNER JOIN
    testing [user] ON
    [user].coords.STDistance( [near].coords ) < (20 * 1609.344)
WHERE
    [user].id = 1
+1

?:

 DECLARE @g geography;
 SELECT @g = coords FROM testing WHERE id=1;        
 SELECT * FROM testing WHERE coords.STDistance(@g) <=(20 * 1609.344)
+1

... , ?

sql1 = "SELECT coords.Lat, coords.Long FROM testing WHERE id=1"
lat2 = rs(0)
lon2 = rs(1)
sql2 = "SELECT * FROM testing WHERE id <> 1 AND coords.STDistance(geography::Point(" & lat2 & ", " & lon2 & ", 4326)) <=(20 * 1609.344)"
'20 miles or less
0

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


All Articles