SLICE_LEN()

All functions > ARRAY > SLICE_LEN()

Returns a slice of an array using start:length:step semantics.

Signatures

Returns: The specified slice of elements or characters

SLICE_LEN(array: ARRAY<T> or VARCHAR, start: BIGINT, [length: BIGINT], [step: BIGINT]) → ARRAY<T> or VARCHAR
sql
ParameterTypeRequiredDescription
arrayARRAY<T> or VARCHARYesThe input array or string to slice
startBIGINTYesStarting position (1-based indexing, negative for end-relative)
lengthBIGINTNoNumber of elements or characters to extract
stepBIGINTNoStep size for element selection (default: 1)

Notes

  • Canonical name is SLICE_LEN. Bare SLICE is a permissive alias; FORMAT (FIX) rewrites it
  • Inclusive-end slicing is SLICE_END or arr[a:b]. Do not write ARRAY_SLICE or LIST_SLICE
  • Uses 1-based indexing (SQL standard)
  • Also slices VARCHAR with the same start/length contract
  • 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

  • SLICE

See also

Examples

Basic Slicing

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

Step Parameter

FeatureQL
RETURN
    -- Step of 1 (same as no step)
    f1 := SLICE_LEN(ARRAY(1, 2, 3, 4, 5, 6), 3, 3, 1),
    -- Step of 2, every other element
    f2 := SLICE_LEN(ARRAY(1, 2, 3, 4, 5, 6), 1, 6, 2),
    -- Step of 2, starting from position 2
    f3 := SLICE_LEN(ARRAY(1, 2, 3, 4, 5, 6), 2, 4, 2),
    -- Step of 3, every third element
    f4 := SLICE_LEN(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
RETURN
    -- Negative step=-1, length=3 from position 6
    f1 := SLICE_LEN(ARRAY(1, 2, 3, 4, 5, 6), 6, 3, -1),
    -- Negative step=-1, length=4 from position 4
    f2 := SLICE_LEN(ARRAY(1, 2, 3, 4, 5, 6), 4, 4, -1),
    -- Negative start with negative step
    f3 := SLICE_LEN(ARRAY(1, 2, 3, 4, 5, 6), -1, 3, -1),
    -- Full reverse with negative step
    f4 := SLICE_LEN(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]