Returns the position of the first occurrence of a given element in an array. Positions are 1-based, counted from left to right.
Syntax
bigint array_position(array<T> <a>, T <element>)Parameters
| Parameter | Required | Description |
|---|---|---|
a | Yes | The array to search. T in array<T> specifies the element data type. Supported types: TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, BOOLEAN, DECIMAL, DECIMALVAL, DATE, DATETIME, TIMESTAMP, IntervalDayTime, IntervalYearMonth, STRING, BINARY, VARCHAR, CHAR, ARRAY, STRUCT, and MAP. |
element | Yes | The element to find. Must be the same data type as the elements in a. |
Usage notes
Positions start at 1, not 0.
If
aorelementis null, the function returns null.If
elementis not found ina, the function returns0.
Examples
Example 1: Find the position of 1 in array(3, 2, 1).
-- Returns 3
SELECT array_position(array(3, 2, 1), 1);Example 2: Search for a null element.
-- Returns null
SELECT array_position(array(3, 1, null), null);Related functions
array_position is a complex type function. For other functions that process ARRAY, MAP, STRUCT, and JSON data, see Complex type functions.