STDistance (geography Data Type)
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance
Returns the shortest distance between a point in a geography instance and a point in another geography instance.
STDistance() returns the shortest LineString between two geography types. This is a close approximate to the geodesic distance. The deviation of
STDistance() on common earth models from the exact geodesic distance is no more than .25%. This avoids confusion over the subtle differences between length and distance in geodesic types.
.STDistance ( other_geography )
To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
Is another geography instance from which to measure the distance between the instance on which STDistance() is invoked. If other_geography is an empty set, STDistance() returns null.
SQL Server return type: float
CLR return type: SqlDouble
The result is expressed in the unit of measure defined by the Spatial Reference Identifier (SRID) of spatial data. STDistance() always returns null if the spatial reference IDs (SRIDs) of the geography instances do not match.
Methods on the geography data type that calculate an area or distance will return different results based on the SRID of the instance used in the method. For more information about SRIDs, see Spatial Reference Identifiers (SRIDs).
The following example finds the distance between two geography instances.
DECLARE @g geography; DECLARE @h geography; SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326); SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326); SELECT @g.STDistance(@h);