SQL Flow Field Guide

geometry.STDifference

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

spatial method STDifference (instance.STDifference(...))

What changes

Input
spatial instance (and arguments)
Output or change
geometry
Return type
SQL Server return type: geometry CLR return type: SqlGeometry

Syntax at a glance

.STDifference ( other_geometry )

Work example

Use geometry.STDifference to subtract the shared portion of another shape.

Check matching SRIDs and inspect empty, split, or collection results. Confirm both values use the same SRID before comparing or combining them.

Check your work Removing the overlap leaves area 8 square coordinate units.

Practice SQL

Open in SQL Flow

Open the SQL for a walkthrough. Run the example in SQL Server to check its result.

-- Planar geometry. SRID 0 coordinates here are synthetic units, not metres.
DECLARE @left geometry;
DECLARE @right geometry;
SET @left = geometry::STGeomFromText(N'POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', 0);
SET @right = geometry::STGeomFromText(N'POLYGON((2 0, 6 0, 6 4, 2 4, 2 0))', 0);
SELECT @left.STDifference(@right).STAsText() AS remaining_left_shape, @left.STDifference(@right).STArea() AS remaining_area_in_square_coordinate_units,
       @left.STSrid AS left_srid,
       @right.STSrid AS right_srid,
       @left.STIsValid() AS left_is_valid,
       @right.STIsValid() AS right_is_valid;

Open Microsoft Learn for full syntax, permissions, and details for your version.