Checks whether an array contains a specific element.
Syntax
boolean array_contains(array<T> <a>, value <v>)Parameters
| Parameter | Required | Description |
|---|---|---|
a | Yes | The array to search. T in array<T> specifies the data type of the elements in the array. The elements can be of any data type. |
v | Yes | The element to search for. The value of v must be of the same data type as the elements in array a. |
Return value
Returns a BOOLEAN value. The function returns true if array a contains element v, and false otherwise.
Examples
The following example uses a table named t_table_array that contains the c1 (BIGINT) and t_array (ARRAY<STRING>) columns with the following data:
+------------+--------------------+
| c1 | t_array |
+------------+--------------------+
| 1000 | [k11, 86, k21, 15] |
| 1001 | [k12, 97, k22, 2] |
| 1002 | [k13, 99, k23, 1] |
+------------+--------------------+The following statement checks whether the t_array column contains the string '1':
select c1, array_contains(t_array, '1') from t_table_array;The following result is returned:
+------------+------+
| c1 | _c1 |
+------------+------+
| 1000 | false |
| 1001 | false |
| 1002 | true |
+------------+------+Because the t_array column is of the ARRAY<STRING> type, the function performs a string comparison. The value '1' matches the string element 1 in the array for row c1 = 1002, so the function returns true for that row.
Related functions
ARRAY_CONTAINS 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.