MakeValid (geometry Data Type)

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance

Converts an invalid geometry instance into a geometry instance with a valid Open Geospatial Consortium (OGC) type.

Syntax

  
.MakeValid ()  

Note

To view Transact-SQL syntax for SQL Server 2014 (12.x) and earlier versions, see Previous versions documentation.

Return Types

SQL Server return type: geometry

CLR return type: SqlGeometry

Remarks

This method may cause a change in the type of the geometry instance, as well as cause the points of a geometry instance to shift slightly.

Examples

The first example creates an invalid LineString instance that overlaps itself and uses STIsValid() to confirm that it is an invalid instance. STIsValid() returns the value of 0 for an invalid instance.

DECLARE @g geometry;  
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 1 1, 1 0, 1 1, 2 2)', 0);  
SELECT @g.STIsValid();  

The second example uses MakeValid() to make the instance valid and to test that the instance is indeed valid. STIsValid() returns the value of 1 for a valid instance.

SET @g = @g.MakeValid();  
SELECT @g.STIsValid();  

The third example verifies how the instance has been changed to make it a valid instance.

SELECT @g.ToString();  

In this example, when the LineString instance is selected, the values are returned as a valid MultiLineString instance.

MULTILINESTRING ((0 2, 1 1, 2 2), (1 1, 1 0))  

The following example converts the CircularString instance into a Point instance.

DECLARE @g geometry = 'CIRCULARSTRING(1 1, 1 1, 1 1)';  
SELECT @g.MakeValid().ToString();  

See Also

STIsValid (geometry Data Type)
Extended Methods on Geometry Instances