Array slicing
On this page
Returns a contiguous range of array elements.
Syntax
<array>[<start>:<end>]
<array>[:<end>]
<array>[<start>:]
<array>[:]Arguments
| Argument | Type | Description |
|---|---|---|
<array> | array or any | Array to slice, or an any value containing an array. |
<start> | int | Optional inclusive position, starting at 0. Negative values count from the end. Omitted or NULL starts at the first element. |
<end> | int | Optional exclusive position. Negative values count from the end. Omitted or NULL continues to the end. |
Returns
Returns the same type as <array>: an array, or an any value containing an array.
Bounds are limited to 0 through the array length after resolving negative indices. If the resulting start index is greater than or equal to the end index, returns an empty array.
Returns NULL if the input is NULL or an any value does not contain an array.
Examples
Select the first two and last two steps from an ordered event journey.
VALUES (['cart_viewed', 'checkout_started', 'payment_failed', 'payment_completed'])
SELECT $0 AS journey
SELECT journey[:2] AS opening_steps, journey[-2:] AS closing_steps+------------------------------------+----------------------------------------+
| opening_steps | closing_steps |
+------------------------------------+----------------------------------------+
| ["cart_viewed","checkout_started"] | ["payment_failed","payment_completed"] |
+------------------------------------+----------------------------------------+The end index is exclusive. Negative indexes make the closing slice independent of the journey length.