>declare @City table >( ci_Id int, > ci_desc varchar(60), > ci_pos geography > ) > >insert into @City (ci_Id, ci_desc, ci_pos) >select 1, 'Here', geography::Point(47.77950, -65.7191, 4326) >union all >select 2, 'There', geography::Point(47.5993, -65.6506, 4326) >union all >select 3, 'Brussels', geography::Point(50.00 + 50./60. + 0/3600., 4.0 + 20./60. + 0/3600, 4326) > > >select * from @City > >declare @CenterLat float=47.7795 >declare @CenterLon float=-65.7191 >declare @SearchDistance float=5000 > >-- http://en.wikipedia.org/wiki/Latitude_and_longitude >declare @LatitudinalDegreeKm float = 110.9 >declare @LongitudinalDegreeKm float = 111.3 > >declare @LatMin float = @CenterLat - @SearchDistance/@LatitudinalDegreeKm >declare @LatMax float = @CenterLat + @SearchDistance/@LatitudinalDegreeKm > >declare @LongMin float = @CenterLon - @SearchDistance/@LongitudinalDegreeKm >declare @LongMax float = @CenterLon + @SearchDistance/@LongitudinalDegreeKm > >declare @Here geography = geography::Point(@CenterLat, @CenterLon, 4326) > > > >/* >select *, > ci_pos.Lat as Latitude, > ci_pos.Long as Longitude, > @Here.STDistance(ci_pos)/1000.0 as distanceKm > from @City >*/ >select *, > @Here.STDistance(ci_pos)/1000.0 as distanceKm, > ci_pos.Lat as Latitude, > ci_pos.Long as Longitude, > @LatMin as lat_min, > @LatMax as lat_max, > @LongMin as long_min, > @LongMax as long_max > from @City > >select *, > @Here.STDistance(ci_pos)/1000.0 as distanceKm, > ci_pos.Lat as Latitude, > ci_pos.Long as Longitude, > @LatMin as lat_min, > @LatMax as lat_max, > @LongMin as long_min, > @LongMax as long_max > from @City > where ( ci_pos.Lat between @LatMin and @LatMax ) > and ( ci_pos.Long between @LongMin and @LongMax) > and ( @Here.STDistance(ci_pos)/1000.0 <= @SearchDistance ) > > > >Spatial methods work but an area within a fixed distance from a point is not a rectangular area. Instead try STBuffer(). ie:
DECLARE @area geography; SET @area = @Here.STBuffer( @SearchDistance * 1000 ); select *, @Here.STDistance(ci_pos)/1000.0 as distanceKm, ci_pos.Lat as Latitude, ci_pos.Long as Longitude from @City where @area.STIntersects( ci_pos ) = 1;Cetin