Sunday, December 31, 2017

Calculate Distance Between Two Lat Long in SQL Server


SQL SERVER provides the geometry data type which stores the spatial data type. To calculate the distance between two lat longs in sql server, the point should be stored in "geometry" data type.

Syntax:
  
     
        SELECT GEOMETRY.STDistance(GEOMETRY)
           

STDistance returns the shortest line distance between two points in a geometry.


Example 1:
STDistance() on numeric column.

@sourceLAT VARCHAR(10),
@sourceLONG VARCHAR(10),
@destinationGEO GEOGRAPHY,
@destinationLAT VARCHAR(10),
@destinationLONG VARCHAR(10)

SET @sourceLAT = '28.412676'
SET @sourceLONG = '77.314813'
SET @sourceGEO = geography::Point(@sourceLAT, @sourceLONG, 4326)

SET @destinationLAT = '28.413686'
SET @destinationLONG = '77.357664'
SET @destinationGEO = geography::Point(@destinationLAT, @destinationLONG, 4326)

SELECT
          ROUND(@sourceGEO.STDistance(@destinationGEO), 2) AS [DISTANCE (Meter)],
          ROUND((@sourceGEO.STDistance(@destinationGEO)) / 1000, 2) AS [DISTANCE (KM)]



Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.

0 comments: