NVL
functionsupportedCore SQL functionsIf expr1 is NULL, returns expr2, otherwise returns expr1. Aliases: IFNULL
Syntax
NVL( <expr1> , <expr2> )
Parameters
| Name | Required | Description |
|---|---|---|
expr1 | required | A general expression. |
expr2 | required | A general expression. |
Returns
Returns the data type of the returned expression. If both expressions are NULL, returns NULL.
Usage notes
- Snowflake performs implicit conversion of arguments to make them compatible. For example, if one of the input expressions is a numeric type, the return type is also a numeric type. That is, SELECT NVL('17', 1); first converts the VARCHAR value '17' to the NUMBER value 17, and then returns the first non-NULL value. When conversion isn’t possible, implicit conversion fails. For example, SELECT NVL('foo', 1); returns an error because the VARCHAR value 'foo' can’t be converted to a NUMBER value. We recommend passing in arguments of the same type or explicitly converting arguments if needed.
- When implicit conversion converts a non-numeric value to a numeric value, the result is a value of type NUMBER(18,5). For numeric string arguments that aren’t constants, if NUMBER(18,5) isn’t sufficient to represent the numeric value, then cast the argument to a type that can represent the value.
- Either expression can include a SELECT statement containing set operators, such as UNION, INTERSECT, EXCEPT, and MINUS. When using set operators, make sure that data types are compatible. For details, see the General usage notes in the Set operators topic.
Collation details
- The collation specifications of all input arguments must be compatible. - The collation of the result of the function is the highest-precedence collation of the inputs.