SQL Server Geometric Functions

Overview

The geometry data type stores planar spatial data. SQL Server provides a rich set of methods for performing geometric calculations and analysis on geometry instances.

Geometric Functions

Function Description Syntax
STArea() Returns the area of the geometry instance. geom.STArea()
STLength() Returns the length of the geometry instance. geom.STLength()
STIsEmpty() Indicates whether the instance is empty. geom.STIsEmpty()
STIsClosed() Specifies if a LineString is closed. geom.STIsClosed()
STNumPoints() Returns the number of points in the instance. geom.STNumPoints()
STPointN(n) Returns the nth point of the instance. geom.STPointN(n)
STConvexHull() Returns the smallest convex geometry that encloses the instance. geom.STConvexHull()
STIntersection(geom2) Returns the geometric intersection of two instances. geom1.STIntersection(geom2)
STDifference(geom2) Returns the part of the first instance that does not intersect the second. geom1.STDifference(geom2)
STUnion(geom2) Returns a geometry that combines two instances. geom1.STUnion(geom2)
STBuffer(distance) Creates a buffer area around the geometry. geom.STBuffer(distance)
STEnvelope() Returns the rectangular envelope of the instance. geom.STEnvelope()
STAsText() Returns the Well-Known Text (WKT) representation. geom.STAsText()
STGeomFromText(wkt, SRID) Creates a geometry instance from WKT. geometry::STGeomFromText(wkt, SRID)
STPointFromText(wkt, SRID) Creates a point from WKT. geometry::STPointFromText(wkt, SRID)

Examples

DECLARE @g geometry = geometry::STGeomFromText('POLYGON((0 0,0 10,10 10,10 0,0 0))',0);
SELECT @g.STArea() AS Area,
       @g.STLength() AS Perimeter,
       @g.STEnvelope().STAsText() AS Envelope;