Spatial SQL -> MS-SQL. Query table by Geometry type

Sometimes spatial data in the table includes mix of geometry types, but we need to select specific one.

-- query table by Geometry type
-- spatial column name in this example is "geog"


-- [query 1] see what Geometry types exists in table

SELECT geog.STGeometryType(), count(*) AS n
FROM dbo.my_table
GROUP BY geog.STGeometryType();

-- [query 2] select only Polygons

SELECT *
FROM dbo.my_table
WHERE geog.STGeometryType() = 'Polygon';

-- [query 3] select only Polygons into New Table ("save as")

SELECT * INTO my_polygons
FROM dbo.my_table
WHERE geog.STGeometryType() = 'Polygon';



sqlexamples.info