DECLARE @g geography; >>SET @g1 = geography::STGeomFromText('POINT(-65.7142 47.7661)', 4326); --Petit-Rocher nord >>SET @g2 = geography::STGeomFromText('POINT(-65.7305 47.7975)', 4326); --Petit-Rocher sud >> >>select @g1.STDistance(@g2); >> >>SELECT @g1.STBuffer(3700).STIntersects(@g2); -- true, within 3700 metersCetin
>DECLARE @CenterLat Float=47.7795 >DECLARE @CenterLon Float=-65.7191 >DECLARE @SearchDistance Float=250 > >SELECT Result.Latitude,Result.Longitude > FROM (SELECT * FROM Test T > CROSS APPLY (SELECT SIN(@CenterLat/57.2957795130823)*SIN(Latitude/57.2957795130823)+ > COS(@CenterLat/57.2957795130823)*COS(Latitude/57.2957795130823)* > COS(Longitude/57.2957795130823-@CenterLon/57.2957795130823) AS Temp) Temp > WHERE (Latitude>=@CenterLat-@SearchDistance/111.0 AND Latitude<=@CenterLat+@SearchDistance/111.0) AND > (Longitude>=@CenterLon-@SearchDistance/111.0 AND Longitude<=@CenterLon+@SearchDistance/111.0)) Result > WHERE 3958.75586574*ACOS(CASE WHEN Temp > 1 THEN 1 WHEN Temp < -1 THEN -1 ELSE Temp END)<=@SearchDistance >>
DECLARE @g geography, @area geography ; SET @g = geography::Point(47.7795,-65.7191,4326) ; --Petit-Rocher SET @area = @g.STBuffer(250) ; SELECT locationId, Latitude, Longitude, @g.STDistance(geoPoint) AS distance FROM ( SELECT locationId, Latitude, Longitude, geography::Point(Latitude,Longitude,4326) AS geoPoint FROM result ) geoData WHERE @area.STIntersects(geoData.geoPoint) = 1 ;PS: If you need more speed then consider storing as spatial data and indexing spatial data as explained in SQL 2008 documentation.