views:

307

answers:

2

I have a SQL Server 2008 table with a column of the geography datatype. The value is a point (latitude and longitude).

How do I query the table to return all rows where the location is within a 10 kilometer radius of a given coordinate?

A: 

You probably want the STDistance method: http://msdn.microsoft.com/en-us/library/bb933952.aspx

Or the STWithin method: http://msdn.microsoft.com/en-us/library/bb933991.aspx

lucas-insasho
Yeah, I was returning to SO to delete the question again: Using the STDistance method is doing the trick, pretty simple. Thanks for the quick response. :) I'll post my "solution" here in a few minutes...
Jakob Gade
A: 

This query eventually solved my problem:

DECLARE @geoMyPoint geography
SET @geoMyPoint = geography::STGeomFromText('POINT(56.5667 9.0333)', 4326);

SELECT vchZipCode, nvcCity, vchLat, vchLong,
  (geography::STGeomFromText('POINT(' + vchLat + ' ' + vchLong + ')', 4326)).STDistance(@geoMyPoint) 
FROM MyTable
Jakob Gade

related questions