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
ParameterTypeRequiredDescription
arrayARRAY<T>YesThe input array to slice
startBIGINTYesStarting position (1-based indexing, negative for end-relative)
lengthBIGINTNoNumber of elements to extract
stepBIGINTNoStep 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_SLICE

  • LIST_SLICE

See also

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 ARRAYf2 ARRAYf3 ARRAYf4 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 ARRAYf2 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 ARRAYf2 ARRAYf3 ARRAYf4 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 ARRAYf2 ARRAYf3 ARRAYf4 ARRAY
[6, 5, 4][4, 3, 2, 1][6, 5, 4][6, 5, 4, 3, 2, 1]

Last update at: 2026/05/26 17:22:09