Copies the elements in an array from the start position based on a specific length and returns the elements as a new array.
Syntax
array<T> slice(array<T> <a>, <start>, <length>)
Parameters
a: required. This parameter specifies an array.
T
inarray<T>
specifies the data type of the elements in the array. The elements can be of any data type.start: required. This parameter specifies the position at which the function starts to copy elements from left to right. The minimum positive value of this parameter is 1. You can also set the start parameter to a negative value. In this case, the start position is counted from the end of the array, but the elements are still copied from left to right.
length: required. The number of elements in the returned array. The value must be greater than or equal to 0. If the value is greater than the length of the input array, this function returns a new array that consists of the elements from the start position to the end of the input array.
Return value
A value of the ARRAY type is returned.
Examples
Example 1: Copy the elements in
array(10, 20, 20, null, null, 30)
from position1
based on a length of3
. Sample statement:-- The return value is [10, 20, 20]. select slice(array(10, 20, 20, null, null, 30), 1, 3);
Example 2: Copy the elements in
array(10, 20, 20, null, null, 30)
from position-2
based on a length of2
. Sample statement:-- The return value is [null, 30]. select slice(array(10, 20, 20, null, null, 30), -2, 2);
Example 3: Copy the elements in
array(10, 20, 20, null, null, 30)
from position3
based on a length of10
. Sample statement:-- The return value is [20, null, null, 30]. select slice(array(10, 20, 20, null, null, 30), 3, 10);
Example 4: Copy the elements in
array(10, 20, 20, null, null, 30)
from position3
based on a length of0
. Sample statement:-- The return value is []. select slice(array(10, 20, 20, null, null, 30), 3, 0);
Related functions
SLICE is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.