ST_Length

ST_Length takes a geometry of type ST_LineString or ST_MultiLineString and, optionally, a unit as input parameters and returns the length of the given geometry either in the default or given unit of measure.

If the given geometry is null or empty, null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_Length(geometry,unit)

Parameter

geometry
A value of type ST_Linestring or ST_MultiLineString that represents the geometry for which the length is returned.
unit
A VARCHAR(128) value that identifies the units in which the length of the curve is measured. The supported units of measure are listed in the DB2GSE.ST_UNITS_OF_MEASURE catalog view.
If the unit parameter is omitted, the following rules are used to determine the unit in which the area is measured:
  • If geometry is in a projected or geocentric coordinate system, the linear unit associated with this coordinate system is used.
  • If geometry is in a geographic coordinate system, the angular unit associated with this coordinate system is used.
Restrictions on unit conversions: An error (SQLSTATE 38SU4) is returned if any of the following conditions occur:
  • The geometry is in an unspecified coordinate system and the unit parameter is specified.
  • The geometry is in a projected coordinate system and an angular unit is specified.
  • The geometry is in a geographic coordinate system, and a linear unit is specified.

Return type

DOUBLE

Examples

Example 1

The following SQL statements create the SAMPLE_GEOMETRIES table and insert a line and a multiline into the table.
SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse   
CREATE TABLE sample_geometries(id SMALLINT, spatial_type varchar(20),      
  geometry ST_GEOMETRY)  
INSERT INTO sample_geometries(id, spatial_type, geometry) 
VALUES (1110, 'ST_LineString', ST_Geometry(ST_LineString
                         ('linestring(50 10, 50 20)', 1))), 
INSERT INTO sample_geometries(id, spatial_type, geometry) 
VALUES (1111, 'ST_MultiLineString', ST_Geometry(ST_MultiLineString
                         ('multilinestring((33 2, 34 3, 35 6), 
                         (28 4, 29 5, 31 8, 43 12), 
                         (39 3, 37 4, 36 7))', 1)))

Example 2

The following SELECT statement calculates the length of the line in the SAMPLE_GEOMETRIES table.
SELECT id, spatial_type, cast(ST_Length(geometry) 
  AS DECIMAL(7, 2)) AS "Line Length" 
FROM   sample_geometries 
WHERE  id = 1110

Example 3

The following SELECT statement calculates the length of the multiline in the SAMPLE_GEOMETRIES table.
SELECT id, spatial_type, ST_Length(geometry)
  AS "multiline_length" 
FROM   sample_geometries 
WHERE  id = 1111
Results:
  ID     SPATIAL_TYPE         MULTILINE_LENGTH  
------ -------------------- ------------------------    
  1111 ST_MultiLineString     +2.76437123387202E+001