ARRAY_POSITION()
All functions > ARRAY > ARRAY_POSITION()
Returns the 1-based position of the first occurrence of an element in an array.
Signatures
Returns: The 1-based position of the first occurrence, or NULL if not found
ARRAY_POSITION(array: ARRAY<T>, element: T) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
array | ARRAY<T> | Yes | The array to search in |
element | T | Yes | The element to find in the array |
Notes
- Returns 1-based index (SQL standard)
- Finds only the first occurrence of the element
- Returns NULL if element is not found in the array
- Performs exact match comparison (case-sensitive for strings)
- Can find NULL values in arrays
- Supports
ARRAY<ROW>andARRAY<ARRAY<T>>: element and array must share the exact same type; mismatched schemas raise a user error
See also
Examples
FeatureQL
SELECT
-- Find element at position 3
f1 := ARRAY_POSITION(ARRAY(1, 2, 3, 4), 3),
-- Find string element
f2 := ARRAY_POSITION(ARRAY('A', 'B', 'C'), 'B'),
-- First occurrence only
f3 := ARRAY_POSITION(ARRAY('apple', 'banana', 'apple'), 'apple'),
-- Element not found
f4 := ARRAY_POSITION(ARRAY(1, 2, 3), 5),
-- Find NULL element
f5 := ARRAY_POSITION(ARRAY(1, NULL::BIGINT, 3), NULL::BIGINT),
-- Empty array
f6 := ARRAY_POSITION(ARRAY()::BIGINT[], 1),
-- First occurrence of duplicate
f7 := ARRAY_POSITION(ARRAY(1, 2, 2, 3), 2)
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT | f5 BIGINT | f6 BIGINT | f7 BIGINT |
|---|---|---|---|---|---|---|
| 3 | 2 | 1 | null | 2 | null | 2 |
On this page