- Categories:
Semi-structured and structured data functions (Array/Object)
ARRAY_CONTAINS¶
Returns TRUE if the specified value is found in the specified array.
Syntax¶
ARRAY_CONTAINS( <value_expr> , <array> )
Arguments¶
value_expr
Value to find in
array
.If
array
is a semi-structured array,value_expr
must evaluate to a VARIANT.If
array
is a structured array,value_expr
must evaluate to a type that is comparable to the type of the array.
array
The array to search.
Returns¶
This function returns a value of BOOLEAN type or NULL:
The function returns TRUE if
value_expr
is present inarray
.The function returns FALSE if
value_expr
is not present inarray
.The function returns NULL if the
value_expr
argument is NULL and there are no NULL values in the array.If the
value_expr
argument is NULL and there is a NULL value in the array, the function returns TRUE.
Usage notes¶
The function does not support wildcards in value_expr
. However, you can
use the ARRAY_TO_STRING function to convert an array to a string, then search the
string with wildcard characters. For example, you can specify wildcards to search the
returned string using the [ NOT ] LIKE and REGEXP_LIKE functions.
Examples¶
The following queries use the ARRAY_CONTAINS function in a SELECT list:
SELECT ARRAY_CONTAINS('hello'::VARIANT, ARRAY_CONSTRUCT('hello', 'hi'));
+------------------------------------------------------------------+
| ARRAY_CONTAINS('HELLO'::VARIANT, ARRAY_CONSTRUCT('HELLO', 'HI')) |
|------------------------------------------------------------------|
| True |
+------------------------------------------------------------------+
SELECT ARRAY_CONTAINS('hello'::VARIANT, ARRAY_CONSTRUCT('hola', 'bonjour'));
+----------------------------------------------------------------------+
| ARRAY_CONTAINS('HELLO'::VARIANT, ARRAY_CONSTRUCT('HOLA', 'BONJOUR')) |
|----------------------------------------------------------------------|
| False |
+----------------------------------------------------------------------+
SELECT ARRAY_CONTAINS(NULL, ARRAY_CONSTRUCT('hola', 'bonjour'));
+----------------------------------------------------------+
| ARRAY_CONTAINS(NULL, ARRAY_CONSTRUCT('HOLA', 'BONJOUR')) |
|----------------------------------------------------------|
| NULL |
+----------------------------------------------------------+
SELECT ARRAY_CONTAINS(NULL, ARRAY_CONSTRUCT('hola', NULL));
+-----------------------------------------------------+
| ARRAY_CONTAINS(NULL, ARRAY_CONSTRUCT('HOLA', NULL)) |
|-----------------------------------------------------|
| True |
+-----------------------------------------------------+
The following query uses the ARRAY_CONTAINS function in a WHERE clause. First, create a table with an ARRAY column and insert data:
CREATE OR REPLACE TABLE array_example (id INT, array_column ARRAY);
INSERT INTO array_example (id, array_column)
SELECT 1, ARRAY_CONSTRUCT(1, 2, 3);
INSERT INTO array_example (id, array_column)
SELECT 2, ARRAY_CONSTRUCT(4, 5, 6);
SELECT * FROM array_example;
+----+--------------+
| ID | ARRAY_COLUMN |
|----+--------------|
| 1 | [ |
| | 1, |
| | 2, |
| | 3 |
| | ] |
| 2 | [ |
| | 4, |
| | 5, |
| | 6 |
| | ] |
+----+--------------+
Run a query that specifies the value to find for value_expr
and the
ARRAY column for array
:
SELECT * FROM array_example WHERE ARRAY_CONTAINS(5, array_column);
+----+--------------+
| ID | ARRAY_COLUMN |
|----+--------------|
| 2 | [ |
| | 4, |
| | 5, |
| | 6 |
| | ] |
+----+--------------+