ARRAY_EXCEPT
functionsupportedCore SQL functionsReturns a new ARRAY that contains the elements from one input ARRAY that are not in another input ARRAY. The function is NULL-safe, meaning it treats NULLs as known values for comparing equality.
Syntax
ARRAY_EXCEPT( <source_array> , <array_of_elements_to_exclude> )
Parameters
| Name | Required | Description |
|---|---|---|
source_array | required | An array that contains elements to be included in the new ARRAY. |
array_of_elements_to_exclude | required | An array that contains elements to be excluded from the new ARRAY. |
Returns
This function returns an ARRAY that contains the elements from source_array that are not in array_of_elements_to_exclude. If no elements remain after excluding the elements in array_of_elements_to_exclude from source_array, the function returns an empty ARRAY. If one or both arguments are NULL, the function returns NULL. The order of the values within the returned array is unspecified.
Usage notes
- When you compare data of the type OBJECT, the objects must be identical to be considered matching. For details, see Examples (in this topic).
- In Snowflake, arrays are multi-sets, not sets. In other words, arrays can contain multiple copies of the same value. ARRAY_EXCEPT compares arrays by using multi-set semantics (sometimes called “bag semantics”). If source_array includes multiple copies of a value, the function only removes the number of copies of that value that are specified in array_of_elements_to_exclude. In other words, if source_array has N copies of a value and array_of_elements_to_exclude has M copies of the same value, the function excludes M copies of the value from the returned array. The number of copies of the value in the returned array is N - M or, if M is larger than N, 0. For example, if source_array contains 5 elements with the value 'A' and array_of_elements_to_exclude contains 2 elements with the value 'A', the returned array contains 3 elements with the value 'A'.
- Both arguments must either be structured ARRAYs or semi-structured ARRAYs.
- If you are passing in a structured ARRAY: The ARRAY in the second argument must be comparable to the ARRAY in the first argument.
- The function returns a structured ARRAY of the same type as the ARRAY in the first argument.