适用于:
Databricks SQL
Databricks Runtime
返回 element 中第一次出现 array 的位置。
语法
array_position(array, element)
参数
array:具有可比较元素的 ARRAY。element:与array中的元素类型匹配的表达式。
返回
一个 BIGINT。
数组的索引编制从 1 开始。 如果元素值 NULL,则返回 NULL。
如果在数组中找不到该元素,则返回 0。
示例
-- 1 exists twice. The function returns the first position
> SELECT array_position(array(3, 2, 1, 4, 1), 1);
3
-- this function cannot be used to find the position of a NULL element.
> SELECT array_position(array(3, NULL, 1), NULL)
NULL
> SELECT array_position(array(3, 2, 1), NULL)
NULL
-- The element is not found in the array
> SELECT array_position(array(3, 2, 1, 4, 1), 5)
0