ST_LineStringN function

The ST_LineStringN function takes a multilinestring and an index as input parameters and returns the linestring that is identified by the index. The resulting linestring is represented in the spatial reference system of the given multilinestring.

If the given multilinestring is null or is empty, or if the index is smaller than 1 or larger than the number of linestrings, then null is returned.

This function can also be called as a method.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_LineStringN(multi_linestring, index)

Parameter

multi_linestring
A value of type ST_MultiLineString that represents the multilinestring from which the linestring that is identified by index is returned.
index
A value of type INTEGER that identifies the nth linestring, which is to be returned from multi_linestring.

If index is smaller than 1 or larger than the number of linestrings in multi_linestring, then null is returned and a warning condition is returned (SQLSTATE 01HS0).

Return type

db2gse.ST_LineString

Example

In the following examples, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display.

The SELECT statement illustrates how to choose the second geometry inside a multilinestring in the SAMPLE_MLINES table.

SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse 

CREATE TABLE sample_mlines (id INTEGER, 
  geometry ST_MULTILINESTRING)

INSERT INTO sample_mlines(id, geometry)
VALUES
    (1110, ST_MultiLineString('multilinestring
                             ((33 2, 34 3, 35 6),
                             (28 4, 29 5, 31 8, 43 12),
                             (39 3, 37 4, 36 7))', 1) ),
    (1111, ST_MLineFromText('multilinestring(
                           (61 2, 64 3, 65 6),
                           (58 4, 59 5, 61 8),
                           (69 3, 67 4, 66 7, 68 9))', 1) )

SELECT id, cast(ST_LineStringN(geometry, 2)..ST_AsText
  AS varchar(110)) AS second_linestring
FROM   sample_mlines 

Results:

ID          SECOND_LINESTRING
----------- -------------------------------------------------------------------       
1110         LINESTRING ( 28.00000000 4.00000000, 29.00000000 
   5.00000000, 31.00000000 8.00000000, 43.00000000 12.00000000)

1111         LINESTRING ( 58.00000000 4.00000000, 59.00000000
   5.00000000, 61.00000000 8.00000000)