Functions for Working with Nullable Values
isNullβ
Checks whether the argument is NULL.
isNull(x)
Alias: ISNULL
.
Arguments
x
β A value with a non-compound data type.
Returned value
1
ifx
isNULL
.0
ifx
is notNULL
.
Example
Input table
ββxββ¬ββββyββ
β 1 β α΄Ία΅α΄Έα΄Έ β
β 2 β 3 β
βββββ΄βββββββ
Query
SELECT x FROM t_null WHERE isNull(y);
ββxββ
β 1 β
βββββ
isNotNullβ
Checks whether the argument is NULL.
isNotNull(x)
Arguments:
x
β A value with a non-compound data type.
Returned value
0
ifx
isNULL
.1
ifx
is notNULL
.
Example
Input table
ββxββ¬ββββyββ
β 1 β α΄Ία΅α΄Έα΄Έ β
β 2 β 3 β
βββββ΄βββββββ
Query
SELECT x FROM t_null WHERE isNotNull(y);
ββxββ
β 2 β
βββββ
coalesceβ
Checks from left to right whether NULL
arguments were passed and returns the first non-NULL
argument.
coalesce(x,...)
Arguments:
- Any number of parameters of a non-compound type. All parameters must be compatible by data type.
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.
ββnameββββββ¬βmailββ¬βphoneββββββ¬ββicqββ
β client 1 β α΄Ία΅α΄Έα΄Έ β 123-45-67 β 123 β
β client 2 β α΄Ία΅α΄Έα΄Έ β α΄Ία΅α΄Έα΄Έ β α΄Ία΅α΄Έα΄Έ β
ββββββββββββ΄βββββββ΄ββββββββββββ΄βββββββ
The mail
and phone
fields are of type String, but the icq
field is UInt32
, so it needs to be converted to String
.
Get the first available contact method for the customer from the contact list:
SELECT name, coalesce(mail, phone, CAST(icq,'Nullable(String)')) FROM aBook;
ββnameββββββ¬βcoalesce(mail, phone, CAST(icq, 'Nullable(String)'))ββ
β client 1 β 123-45-67 β
β client 2 β α΄Ία΅α΄Έα΄Έ β
ββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ifNullβ
Returns an alternative value if the main argument is NULL
.
ifNull(x,alt)
Arguments:
x
β The value to check forNULL
.alt
β The value that the function returns ifx
isNULL
.
Returned values
- The value
x
, ifx
is notNULL
. - The value
alt
, ifx
isNULL
.
Example
SELECT ifNull('a', 'b');
ββifNull('a', 'b')ββ
β a β
ββββββββββββββββββββ
SELECT ifNull(NULL, 'b');
ββifNull(NULL, 'b')ββ
β b β
βββββββββββββββββββββ
nullIfβ
Returns NULL
if the arguments are equal.
nullIf(x, y)
Arguments:
x
, y
β Values for comparison. They must be compatible types, or ClickHouse will generate an exception.
Returned values
NULL
, if the arguments are equal.- The
x
value, if the arguments are not equal.
Example
SELECT nullIf(1, 1);
ββnullIf(1, 1)ββ
β α΄Ία΅α΄Έα΄Έ β
ββββββββββββββββ
SELECT nullIf(1, 2);
ββnullIf(1, 2)ββ
β 1 β
ββββββββββββββββ
assumeNotNullβ
Results in an equivalent non-Nullable
value for a Nullable type. In case the original value is NULL
the result is undetermined. See also ifNull
and coalesce
functions.
assumeNotNull(x)
Arguments:
x
β The original value.
Returned values
- The original value from the non-
Nullable
type, if it is notNULL
. - Implementation specific result if the original value was
NULL
.
Example
Consider the t_null
table.
SHOW CREATE TABLE t_null;
ββstatementββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CREATE TABLE default.t_null ( x Int8, y Nullable(Int8)) ENGINE = TinyLog β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββxββ¬ββββyββ
β 1 β α΄Ία΅α΄Έα΄Έ β
β 2 β 3 β
βββββ΄βββββββ
Apply the assumeNotNull
function to the y
column.
SELECT assumeNotNull(y) FROM t_null;
ββassumeNotNull(y)ββ
β 0 β
β 3 β
ββββββββββββββββββββ
SELECT toTypeName(assumeNotNull(y)) FROM t_null;
ββtoTypeName(assumeNotNull(y))ββ
β Int8 β
β Int8 β
ββββββββββββββββββββββββββββββββ
toNullableβ
Converts the argument type to Nullable
.
toNullable(x)
Arguments:
x
β The value of any non-compound type.
Returned value
- The input value with a
Nullable
type.
Example
SELECT toTypeName(10);
ββtoTypeName(10)ββ
β UInt8 β
ββββββββββββββββββ
SELECT toTypeName(toNullable(10));
ββtoTypeName(toNullable(10))ββ
β Nullable(UInt8) β
ββββββββββββββββββββββββββββββ