Functions for working with nullable values
isNull
Returns whether the argument is NULL.
See also operator IS NULL
.
Syntax
Alias: ISNULL
.
Arguments
x
— A value of non-compound data type.
Returned value
1
ifx
isNULL
.0
ifx
is notNULL
.
Example
Table:
Query:
Result:
isNullable
Returns 1
if a column is Nullable (i.e allows NULL
values), 0
otherwise.
Syntax
Arguments
x
— column.
Returned value
Example
Query:
Result:
isNotNull
Returns whether the argument is not NULL.
See also operator IS NOT NULL
.
Arguments:
x
— A value of non-compound data type.
Returned value
1
ifx
is notNULL
.0
ifx
isNULL
.
Example
Table:
Query:
Result:
isNotDistinctFrom
Performs null-safe comparison. Used to compare JOIN keys which contain NULL values in the JOIN ON section.
This function will consider two NULL
values as identical and will return true
, which is distinct from the usual
equals behavior where comparing two NULL
values would return NULL
.
This function is an internal function used by the implementation of JOIN ON. Please do not use it manually in queries.
Syntax
Arguments
x
— first JOIN key.y
— second JOIN key.
Returned value
true
whenx
andy
are bothNULL
.false
otherwise.
Example
For a complete example see: NULL values in JOIN keys.
isZeroOrNull
Returns whether the argument is 0 (zero) or NULL.
Arguments:
x
— A value of non-compound data type.
Returned value
1
ifx
is 0 (zero) orNULL
.0
else.
Example
Table:
Query:
Result:
coalesce
Returns the leftmost non-NULL
argument.
Arguments:
- Any number of parameters of non-compound type. All parameters must be of mutually compatible data types.
Returned values
- The first non-
NULL
argument NULL
, if all arguments areNULL
.
Example
Consider a list of contacts that may specify multiple ways to contact a customer.
The mail
and phone
fields are of type String, but the telegram
field is UInt32
, so it needs to be converted to String
.
Get the first available contact method for the customer from the contact list:
ifNull
Returns an alternative value if the argument is NULL
.
Arguments:
x
— The value to check forNULL
.alt
— The value that the function returns ifx
isNULL
.
Returned values
x
ifx
is notNULL
.alt
ifx
isNULL
.
Example
Query:
Result:
Query:
Result:
nullIf
Returns NULL
if both arguments are equal.
Arguments:
x
, y
— Values to compare. Must be of compatible types.
Returned values
NULL
if the arguments are equal.x
if the arguments are not equal.
Example
Query:
Result:
Query:
Result:
assumeNotNull
Returns the corresponding non-Nullable
value for a value of Nullable type. If the original value is NULL
, an arbitrary result can be returned. See also functions ifNull
and coalesce
.
Arguments:
x
— The original value.
Returned values
- The input value as non-
Nullable
type, if it is notNULL
. - An arbitrary value, if the input value is
NULL
.
Example
Table:
Query:
Result:
Query:
Result:
toNullable
Converts the argument type to Nullable
.
Arguments:
x
— A value of non-compound type.
Returned value
- The input value but of
Nullable
type.
Example
Query:
Result:
Query:
Result:
assumeNotNull
Introduced in: v1.1
Returns the corresponding non-Nullable
value for a value of type Nullable
.
If the original value is NULL
, an arbitrary result can be returned.
See also: functions ifNull
and coalesce
.
Syntax
Arguments
x
— The original value of any nullable type.Nullable(T)
Returned value
Returns the non-nullable value, if the original value was not NULL
, otherwise an arbitrary value, if the input value is NULL
. Any
Examples
Usage example
coalesce
Introduced in: v1.1
Returns the leftmost non-NULL
argument.
Syntax
Arguments
x[, y, ...]
— Any number of parameters of non-compound type. All parameters must be of mutually compatible data types.Any
Returned value
Returns the first non-NULL
argument, otherwise NULL
, if all arguments are NULL
. Any
or NULL
Examples
Usage example
ifNull
Introduced in: v1.1
Returns an alternative value if the first argument is NULL
.
Syntax
Arguments
Returned value
Returns the value of x
if it is not NULL
, otherwise alt
. Any
Examples
Usage example
isNotDistinctFrom
Introduced in: v23.8
Performs a null-safe comparison between two JOIN
keys. This function will consider
two NULL
values as identical and will return true
, which is distinct from the usual
equals behavior where comparing two NULL
values would return NULL
.
This function is an internal function used by the implementation of JOIN ON
.
Please do not use it manually in queries.
For a complete example see: NULL
values in JOIN
keys.
Syntax
Arguments
Returned value
Returns true
when x
and y
are both NULL
, otherwise false
. Bool
Examples
isNotNull
Introduced in: v1.1
Checks if the argument is not NULL
.
Also see: operator IS NOT NULL
.
Syntax
Arguments
x
— A value of non-compound data type.Any
Returned value
Returns 1
if x
is not NULL
, otherwise 0
. UInt8
Examples
Usage example
isNull
Introduced in: v1.1
Checks if the argument is NULL
.
Also see: operator IS NULL
.
Syntax
Arguments
x
— A value of non-compound data type.Any
Returned value
Returns 1
if x
is NULL
, otherwise 0
. UInt8
Examples
Usage example
isNullable
Introduced in: v22.7
Checks whether the argument's data type is Nullable
(i.e it allows NULL
values).
Syntax
Arguments
x
— A value of any data type.Any
Returned value
Returns 1
if x
is of a Nullable
data type, otherwise 0
. UInt8
Examples
Usage example
isZeroOrNull
Introduced in: v20.3
Checks if the argument is either zero (0
) or NULL
.
Syntax
Arguments
x
— A numeric value.UInt
Returned value
Returns 1
if x
is NULL
or equal to zero, otherwise 0
. UInt8/16/32/64
or Float32/Float64
Examples
Usage example
nullIf
Introduced in: v1.1
Returns NULL
if both arguments are equal.
Syntax
Arguments
Returned value
Returns NULL
if both arguments are equal, otherwise returns the first argument. NULL
or Nullable(x)
Examples
Usage example
toNullable
Introduced in: v1.1
Converts the provided argument type to Nullable
.
Syntax
Arguments
x
— A value of any non-compound type.Any
Returned value
Returns the input value but of Nullable
type. Nullable(Any)
Examples
Usage example