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
    -- Basic positive start, no length (slice to end)
    f1 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 4),
    -- Basic positive start and length
    f2 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 2, 3),
    -- Start from beginning, positive length
    f3 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 1, 4),
    -- Positive start, length extends past end
    f4 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 4, 5)
;
Result
f1 ARRAYf2 ARRAYf3 ARRAYf4 ARRAY
[4, 5, 6][2, 3, 4][1, 2, 3, 4][4, 5, 6]

Negative Indices

FeatureQL
SELECT
    -- Negative start, no length (slice to end)
    f1 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), -3),
    -- Negative start, positive length
    f2 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), -4, 2)
;
Result
f1 ARRAYf2 ARRAY
[4, 5, 6][3, 4]

Step Parameter

FeatureQL
SELECT
    -- Step of 1 (same as no step)
    f1 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 3, 3, 1),
    -- Step of 2, every other element
    f2 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 1, 6, 2),
    -- Step of 2, starting from position 2
    f3 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 2, 4, 2),
    -- Step of 3, every third element
    f4 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 1, 6, 3)
;
Result
f1 ARRAYf2 ARRAYf3 ARRAYf4 ARRAY
[3, 4, 5][1, 3, 5][2, 4][1, 4]

Negative Step

FeatureQL
SELECT
    -- Negative step=-1, length=3 from position 6
    f1 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 6, 3, -1),
    -- Negative step=-1, length=4 from position 4
    f2 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 4, 4, -1),
    -- Negative start with negative step
    f3 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), -1, 3, -1),
    -- Full reverse with negative step
    f4 := SLICE(ARRAY(1, 2, 3, 4, 5, 6), 6, 6, -1)
;
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/06/20 10:08:10