SQL Flow Field Guide

geometry.STAsBinary

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

spatial method STAsBinary (instance.STAsBinary(...))

What changes

Input
spatial instance (and arguments)
Output or change
varbinary(max)
Return type
SQL Server return type: varbinary(max) CLR return type: SqlBytes

Syntax at a glance

.STAsBinary ( )

Work example

Use geometry.STAsBinary to export OGC Well-Known Binary.

Check whether the consumer needs Z or M ordinates. Keep the geometry type and SRID in the review.

Check your work WKB encodes the coordinates but not SQL Server SRID. The query reports the SRID separately.

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 @shape geometry;
SET @shape = geometry::STGeomFromText(N'POINT(3 4)', 0);
SELECT @shape.STAsBinary() AS point_wkb,
       @shape.STSrid AS source_srid,
       @shape.STIsValid() AS source_is_valid;

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