Search code examples
sqlsql-servergeometrygisgeo

How to query nearby sites by geography?


I use SQL Server 2014 as my database, it has 1 million rows of data in one table, how to query nearby sites by geography column?

Does SQL Server have any such functions?

We should consider about performance.

Example:

select top 10 
from table1 
where nearby(latlng) < 10(miles)

Solution

  • Add a geography column, called Location for example, to the TABLE1. Make sure you create a spatial index for that column to improve performance of spatial queries.

    SELECT TOP 10 * FROM TABLE1
    ORDER BY TABLE1.Location.STDistance(latlng) DESC