STAsEWKT — Implements an method to create Extended Well Known Text (EWKT) strings from the input @p_geometry.


The Open Geospatial Consortium’s, Simple Features Specifications (SFS) 1.1 and 1.2, as well as the SQL/MM standard, define a Well Known Text (WKT) format for use in interchanging or processing geometry data.

These specifications are 2D in nature: they only describe geometry data with X and Y ordinates as follows:

  • POINT(1 1)
  • LINESTRING(0 0,1 1)
  • POLYGON((0 0,10 0,10 10,0 10,0 0))
  • etc

If a geometry object has Z or M ordinates, the current specification does not define a method for describing such data.

There is a need for WKT to support Z and M ordinates, as well as the SRID of the geometry object.

Various vendors and open source projects have defined and implemented their own extensions to the base 2D WKT, for example PostGIS.

However, these implementations, while using a common approach, are not exactly the same.

We give all these implementations the generic name of Extended Well Known Text (EWKT).

The following examples follow PostGIS’s EWKT implementation. Note the encoding of the geometry’s SRID values, as well as Z/M ordinates:

(POINT is used as a generic placeholder)

  • POINT Z (x y z)
  • POINT M (x y m)
  • POINT ZM (x y z m)
  • SRID=28346; POINT (x y)V/li>
  • SRID=28346; POINT Z (x y z)
  • SRID=28346; POINT M (x y m)
  • SRID=28346; POINT ZM (x y z m)

I have written a function, STAsEWKT() (see below), for SQL Server Spatial that extends or wraps its current WKT functionality (it supports Z and M but not SRID encoding) to that it will import PostGIS style EWKT (or other vendor EWKT such as POINT Z (x y z). It also handles situations like POINT M (x y m) where SQL Server encodes such an object as POINT(x y NULL m).

Where a geometry is 2D, the function exports by using the or functions directly.

Function Specification

Function [$(owner)].[STAsEWKT] (
  @p_geometry geometry
Returns varchar(max)


@p_geometry (geometry) -- Any valid or empty geometry object.


This function returns an EWKT string describing @p_geometry with size varchar(max)


select [$(owner)].[STAsEWKT](geometry::STGeomFromText('POINT (1 2)',0));

POINT(1 2)

(1 row affected)

select [$(owner)].[STAsEWKT](geometry::STGeomFromText('POINT (1 2)',2274));

SRID=2274;POINT(1 2)

(1 row affected)

select [$(owner)].[STAsEWKT](geometry::STGeomFromText('POINT (1 2 3)',0));

POINTZ(1 2 3)

(1 row affected)

select [$(owner)].[STAsEWKT](geometry::STGeomFromText('POINT (1 2 3)',2274));

SRID=2274;POINTZ(1 2 3)

(1 row affected)

select [$(owner)].[STAsEWKT](geometry::STGeomFromText('POINT (1 2 NULL 3)',2274));

SRID=2274;POINTM(1 2 3)

(1 row affected)

select [$(owner)].[STAsEWKT](geometry::STGeomFromText('POINT (1 2 1.2 3)',2274));

SRID=2274;POINTZM(1 2 1.2 3)

(1 row affected)


This function was not that difficult to write and, along with STGeomFromEWKT() should provide use tools for importing and exporting geometry data to and from many external systems, from FGDB, qGIS, PostGIS, GeoPackage, Spatialite etc.

This function is shipped with my package of SQL Server Spatial TSQL functions. It is available via donation on this website.

I hope this function is of use to someone out there.