Returns a value from an ARRAY or MAP parameter.
Command format
INDEX (<var1>[<var2>])Notes
If var1 is an
array<T>, this function retrieves the element at index var2 from var1. Array elements are indexed starting from 0.If var1 is a
map<K, V>, this function retrieves the value for the key var2 from var1.
This operation does not use the
INDEXkeyword. Use the<var1>[<var2>]syntax directly. Otherwise, an error is returned.If var1 is an array, var2 is the index. If var1 is a map, var2 is the key.
Parameter description
var1: Required. An
array<T>ormap<K, V>. Inarray<T>, T specifies the data type of the array elements.Tcan be any data type. Inmap<K, V>,KandVrepresent the data types of the keys and values, respectively.var2: Required.
If var1 is an
array<T>, var2 must be a non-negative BIGINT value.If var1 is a
map<K, V>, var2 must have the same data type as K.
Return value description
If var1 is an
array<T>, the function returns a value of type T. The following rules apply:If var2 is out of bounds for the var1 array, the function returns NULL.
If var1 is NULL, the function returns NULL.
If var1 is a
map<K, V>, the function returns a value of type V. The following rules apply:If the key var2 does not exist in the
map<K, V>, the function returns NULL.If var1 is NULL, the function returns NULL.
Usage examples
Example 1: var1 is an
array<T>.-- Returns c. SELECT ARRAY('a','b','c')[2];Example 2: var1 is a
map<K, V>.-- Returns 1. SELECT STR_TO_MAP("test1=1,test2=2")["test1"];
Related functions
INDEX is a complex type function. For more information about functions that process complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.