- Categories:
Semi-structured and structured data functions (Array/Object)
ARRAY_ CONTAINS¶
Returns TRUE if the specified value is found in the specified array.
Syntax¶
Arguments¶
value_exprValue to find in
array.- If
arrayis a semi-structured array,value_exprmust evaluate to a VARIANT. - If
arrayis a structured array,value_exprmust evaluate to a type that is comparable to the type of the array.
- If
arrayThe array to search.
Returns¶
This function returns a value of BOOLEAN type or NULL:
-
The function returns TRUE if
value_expris present inarray, including the following cases:- When the
value_exprargument is NULL and there is a SQL NULL value in the array (undefined). - When the
value_exprargument is JSON null and there is a JSON null value in the array (null).
- When the
-
The function returns FALSE if
value_exprisn’t present inarray, including when thevalue_exprargument is JSON null and there are no JSON null values in the array. -
The function returns NULL if the
value_exprargument is NULL and there are no SQL NULL values in the array.
For more information about NULL values in arrays, see NULL values.
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. -
If
arrayis a semi-structured array, explicit casting of thevalue_exprvalue to a VARIANT value is required for values of the following data types:The following example explicitly casts a string value to a VARIANT value:
Explicit casting isn’t required for values of other data types.
Examples¶
The following queries use the ARRAY_CONTAINS function in a SELECT list.
In this example, the function returns TRUE because the value_expr argument is 'hello'
and the array contains a VARIANT value that stores the string 'hello':
In this example, the function returns FALSE because the value_expr argument is 'hello'
but the array doesn’t contain a VARIANT value that stores the string 'hello':
In this example, the function returns NULL because the value_expr argument is NULL but
the array doesn’t contain a SQL NULL value:
In this example, the function returns TRUE because the value_expr argument is NULL and
the array contains a SQL NULL value:
In this example, the function returns TRUE because the value_expr argument is a
JSON null value and the array contains a JSON null value:
In this example, the function returns NULL because the value_expr argument is
NULL but the array doesn’t contain a SQL NULL value (although it does contain a JSON null value):
The following query uses the ARRAY_CONTAINS function in a WHERE clause. First, create a table with an ARRAY column and insert data:
Run a query that specifies the value to find for value_expr and the
ARRAY column for array: