SQL Flow Field Guide

geometry.ShortestLineTo

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

spatial method ShortestLineTo (instance.ShortestLineTo(...))

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

.ShortestLineTo ( geometry_other )

Work example

Use geometry.ShortestLineTo to draw the shortest connector between two shapes.

Use matching SRIDs and confirm the output coordinate system. Confirm both values use the same SRID before comparing or combining them.

Check your work The shortest segment is 5 planar coordinate units. Both SRIDs must match.

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'POINT(0 0)', 0);
SET @right = geometry::STGeomFromText(N'POINT(3 4)', 0);
SELECT @left.ShortestLineTo(@right).STAsText() AS connecting_segment, @left.ShortestLineTo(@right).STLength() AS distance_in_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.