Trying to find:
Background:
INSERT INTO routes (route_id, route_name, route_geography)
VALUES (
8,
'route-test-8',
geography::STGeomFromText('LINESTRING(50.47959 -80.518649, 50.47923 -80.519601, ......)', 4326)
);
NOTE: For testing purposes, this random point is a point that exists in one of the routes. So in theory I should at least get 1 route in true result
Columns and it's types: route_geography of type geography
Tried the following query:
-- Declare the "random" point
DECLARE @startPoint geography;
SET @startPoint = geography::Point('50.47959','-80.518649' , 4326); -- this is the exact point from one of the routes
-- Create a buffer around the "random" point
DECLARE @buffer geography;
SET @buffer = @startPoint.STBuffer(5000); -- Create a 5km radius buffer
-- Finding the routes that pass through the buffer
SELECT [route_id], [route_name],[route_geography].
FROM [test-DB].[dbo].[routes]
WHERE @buffer.STContains([route_geography]) = 1; -- when assigned 0 returns all the routes
-- Declare the "random" point
DECLARE @startPoint geography;
SET @startPoint = geography::Point('50.47959','-80.518649' , 4326); -- this is the exact point from one of the routes
-- Create a buffer around the "random" point
DECLARE @buffer geography;
SET @buffer = @startPoint.STBuffer(5000); -- Create a 5km radius buffer
-- Finding the routes that pass through the buffer
SELECT [route_id], [route_name]
FROM [test-DB].[dbo].[routes]
WHERE [route_geography].STIntersects(@buffer) = 1; -- when assigned 0 returns all the routes
It's probably because the arguments you are giving to geography::Point
are in the wrong order. The function geography::Point
takes first Lat, then Long. This is different from WKT where coordinates are given as first Long then Lat. This is helpfully pointed out in a note in the documentation: https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/point-geography-data-type?view=sql-server-ver16