SLICE
All functions > ARRAY > SLICE
Returns a slice of an array using start:length:step semantics.
Signatures
Returns: A new array containing the specified slice of elements
SLICE(array: ARRAY<T>, start: BIGINT, [length: BIGINT], [step: BIGINT]) → ARRAY<T> sql
| Parameter | Type | Required | Description |
|---|---|---|---|
array | ARRAY<T> | Yes | The input array to slice |
start | BIGINT | Yes | Starting position (1-based indexing, negative for end-relative) |
length | BIGINT | No | Number of elements to extract |
step | BIGINT | No | Step size for element selection (default: 1) |
Notes
- Uses 1-based indexing (SQL standard)
- Negative start values count from the end of the array
- Optional length parameter specifies how many elements to extract
- Optional step parameter allows extracting every nth element
- Step can be negative for reverse traversal
- Returns empty array if start is out of bounds
Aliases
ARRAY_SLICELIST_SLICE
Examples
Basic Slicing
FeatureQL
SELECT
f1 := SLICE(ARRAY[1,2,3,4,5,6], 4), -- Basic positive start, no length (slice to end)
f2 := SLICE(ARRAY[1,2,3,4,5,6], 2, 3), -- Basic positive start and length
f3 := SLICE(ARRAY[1,2,3,4,5,6], 1, 4), -- Start from beginning, positive length
f4 := SLICE(ARRAY[1,2,3,4,5,6], 4, 5) -- Positive start, length extends past end
;Result
| f1 ARRAY | f2 ARRAY | f3 ARRAY | f4 ARRAY |
|---|---|---|---|
| [4, 5, 6] | [2, 3, 4] | [1, 2, 3, 4] | [4, 5, 6] |
Negative Indices
FeatureQL
SELECT
f1 := SLICE(ARRAY[1,2,3,4,5,6], -3), -- Negative start, no length (slice to end)
f2 := SLICE(ARRAY[1,2,3,4,5,6], -4, 2) -- Negative start, positive length
;Result
| f1 ARRAY | f2 ARRAY |
|---|---|
| [4, 5, 6] | [3, 4] |
Step Parameter
FeatureQL
SELECT
f1 := SLICE(ARRAY[1,2,3,4,5,6], 3, 3, 1), -- Step of 1 (same as no step)
f2 := SLICE(ARRAY[1,2,3,4,5,6], 1, 6, 2), -- Step of 2, every other element
f3 := SLICE(ARRAY[1,2,3,4,5,6], 2, 4, 2), -- Step of 2, starting from position 2
f4 := SLICE(ARRAY[1,2,3,4,5,6], 1, 6, 3) -- Step of 3, every third element
;Result
| f1 ARRAY | f2 ARRAY | f3 ARRAY | f4 ARRAY |
|---|---|---|---|
| [3, 4, 5] | [1, 3, 5] | [2, 4] | [1, 4] |
Negative Step
FeatureQL
SELECT
f1 := SLICE(ARRAY[1,2,3,4,5,6], 6, 3, -1), -- Negative step=-1, length=3 from position 6
f2 := SLICE(ARRAY[1,2,3,4,5,6], 4, 4, -1), -- Negative step=-1, length=4 from position 4
f3 := SLICE(ARRAY[1,2,3,4,5,6], -1, 3, -1), -- Negative start with negative step
f4 := SLICE(ARRAY[1,2,3,4,5,6], 6, 6, -1) -- Full reverse with negative step
;Result
| f1 ARRAY | f2 ARRAY | f3 ARRAY | f4 ARRAY |
|---|---|---|---|
| [6, 5, 4] | [4, 3, 2, 1] | [6, 5, 4] | [6, 5, 4, 3, 2, 1] |