Skip to main content
Version: 24.3.x

Categories: Semi-Structured Data

ARRAY_SLICE

Returns an array constructed from the specified subset of elements in the input array.

Syntax

ARRAY_SLICE(arr LIST, from int, to int) → LIST

  • arr: The input array.
  • from: The zero-based position in the input array of the first element to include in the output array. Elements in positions that are less than the from position are not included in the output array. A negative position is interpreted as an index from the back of the array. For example, the value -1 begins the output array with the last element in the input array.
  • to: The zero-based position in the input array of the last element to include in the output array. Elements in positions that are equal to or greater than the to position are not included in the resulting array. A negative position is interpreted as an index from the back of the array. For example, the value -1 ends the output array with the second-to-last element in the input array.

Examples

array_col contains ARRAY[0,1,2,3,4,5,6], 0, 3
SELECT ARRAY_SLICE(array_col)
-- [0,1,2]
array_col contains ARRAY[0,1,2,3,4,5,6], 0, -2
SELECT ARRAY_SLICE(array_col)
-- [0,1,2,3,4]
array_col contains ARRAY[0,1,2,3,4,5,6], -5, -3
SELECT ARRAY_SLICE(array_col)
-- [2,3]
array_col contains ARRAY[0,1,2,3,4,5,6], 10, 12
SELECT ARRAY_SLICE(array_col)
-- []

Usage Notes

The output includes elements up to but not including the element specfied by the to parameter.

If the array is NULL, the result is NULL. If either of the from or to parameters is NULL, the result is NULL.

If both of the of the from or to parameters are beyond the upper or lower end of the array, the result is an empty array.