STDifference (geometry Data Type)
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance SQL database in Microsoft Fabric
Returns an object that represents the point set from one geometry instance that does not lie within another geometry instance.
Syntax
.STDifference ( other_geometry )
Arguments
other_geometry
Is another geometry instance indicating which points to remove from the instance on which STDifference()
is being invoked.
Return Types
SQL Server return type: geometry
CLR return type: SqlGeometry
Remarks
This method always returns null if the spatial reference IDs (SRIDs) of the geometry instances do not match. The result may contain circular arc segments only if the input instances contain circular arc segments.
Examples
A. Computing the difference between two Polygon instances
The following example uses STDifference()
to compute the difference between two polygons.
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STDifference(@h).ToString();
B. Invoking STDifference() on a CurvePolygon instance
The following example uses STDifference() on a CurvePolygon instance.
DECLARE @g geometry = 'CURVEPOLYGON (CIRCULARSTRING (0 -4, 4 0, 0 4, -4 0, 0 -4))';
DECLARE @h geometry = 'POLYGON ((1 -1, 5 -1, 5 3, 1 3, 1 -1))';
-- Note the different results returned by the two SELECT statements
SELECT @h.STDifference(@g).ToString(), @g.STDifference(@h).ToString();