Array slicing

Returns a contiguous range of elements from an array.

Syntax

<array>[<start>:<end>]
<array>[:<end>]
<array>[<start>:]
<array>[:]

Arguments

<array>

An expression that evaluates to an ARRAY, or to an ANY value containing an array.

<start>

The zero-based position of the first element to include. A negative value counts backward from the end of the array. When omitted or NULL, the slice starts at the first element.

<end>

The zero-based position at which the slice stops. The element at this position is not included. A negative value counts backward from the end of the array. When omitted or NULL, the slice continues through the final element.

Returns

Returns an ARRAY containing the selected elements. Bounds outside the array are clamped to its valid range. If the normalized start position is greater than or equal to the end position, the result is an empty array.

Returns NULL when <array> is NULL, or when an ANY value does not contain an array.

ScopeQL does not support a step value in slice syntax.

Examples

SELECT ['a', 'b', 'c', 'd', 'e'] AS values
SELECT
    values[1:4] AS middle,
    values[-2:] AS tail,
    values[:2] AS first_two,
    values[:] AS all_values;
+---------------+-----------+-----------+-----------------------+
| middle        | tail      | first_two | all_values          |
+---------------+-----------+-----------+-----------------------+
| ["b","c","d"] | ["d","e"] | ["a","b"] | ["a","b","c","d","e"] |
+---------------+-----------+-----------+-----------------------+