2020-04-03 13:23:32 +00:00
---
toc_priority: 63
2020-06-19 10:21:15 +00:00
toc_title: Nullable
2020-04-03 13:23:32 +00:00
---
2020-06-19 10:21:15 +00:00
# Functions for Working with Nullable Values {#functions-for-working-with-nullable-aggregates}
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
## isNull {#isnull}
2018-09-04 11:18:59 +00:00
2020-05-06 06:13:29 +00:00
Checks whether the argument is [NULL ](../../sql-reference/syntax.md#null-literal ).
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
isNull(x)
```
2021-02-16 10:53:44 +00:00
Alias: `ISNULL` .
2021-02-15 21:22:10 +00:00
**Arguments**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- `x` — A value with a non-compound data type.
2018-09-04 11:18:59 +00:00
**Returned value**
2020-03-21 04:11:51 +00:00
- `1` if `x` is `NULL` .
- `0` if `x` is not `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
Input table
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
```
Query
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT x FROM t_null WHERE isNull(y);
2018-09-04 11:18:59 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┐
│ 1 │
└───┘
```
2020-03-20 10:10:48 +00:00
## isNotNull {#isnotnull}
2018-09-04 11:18:59 +00:00
2020-05-06 06:13:29 +00:00
Checks whether the argument is [NULL ](../../sql-reference/syntax.md#null-literal ).
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
isNotNull(x)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- `x` — A value with a non-compound data type.
2018-09-04 11:18:59 +00:00
**Returned value**
2020-03-21 04:11:51 +00:00
- `0` if `x` is `NULL` .
- `1` if `x` is not `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
Input table
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
```
Query
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT x FROM t_null WHERE isNotNull(y);
2018-09-04 11:18:59 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┐
│ 2 │
└───┘
```
2020-03-20 10:10:48 +00:00
## coalesce {#coalesce}
2018-09-04 11:18:59 +00:00
Checks from left to right whether `NULL` arguments were passed and returns the first non-`NULL` argument.
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
coalesce(x,...)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- Any number of parameters of a non-compound type. All parameters must be compatible by data type.
2018-09-04 11:18:59 +00:00
**Returned values**
2020-03-21 04:11:51 +00:00
- The first non-`NULL` argument.
- `NULL` , if all arguments are `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
Consider a list of contacts that may specify multiple ways to contact a customer.
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─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:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT coalesce(mail, phone, CAST(icq,'Nullable(String)')) FROM aBook;
2018-09-04 11:18:59 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─name─────┬─coalesce(mail, phone, CAST(icq, 'Nullable(String)'))─┐
│ client 1 │ 123-45-67 │
│ client 2 │ ᴺᵁᴸᴸ │
└──────────┴──────────────────────────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## ifNull {#ifnull}
2018-09-04 11:18:59 +00:00
Returns an alternative value if the main argument is `NULL` .
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
ifNull(x,alt)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- `x` — The value to check for `NULL` .
- `alt` — The value that the function returns if `x` is `NULL` .
2018-09-04 11:18:59 +00:00
**Returned values**
2020-03-21 04:11:51 +00:00
- The value `x` , if `x` is not `NULL` .
- The value `alt` , if `x` is `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT ifNull('a', 'b');
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─ifNull('a', 'b')─┐
│ a │
└──────────────────┘
```
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT ifNull(NULL, 'b');
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─ifNull(NULL, 'b')─┐
│ b │
└───────────────────┘
```
2020-03-20 10:10:48 +00:00
## nullIf {#nullif}
2018-09-04 11:18:59 +00:00
Returns `NULL` if the arguments are equal.
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
nullIf(x, y)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
`x` , `y` — Values for comparison. They must be compatible types, or ClickHouse will generate an exception.
**Returned values**
2020-03-21 04:11:51 +00:00
- `NULL` , if the arguments are equal.
- The `x` value, if the arguments are not equal.
2018-09-04 11:18:59 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT nullIf(1, 1);
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─nullIf(1, 1)─┐
│ ᴺᵁᴸᴸ │
└──────────────┘
```
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT nullIf(1, 2);
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─nullIf(1, 2)─┐
│ 1 │
└──────────────┘
```
2020-03-20 10:10:48 +00:00
## assumeNotNull {#assumenotnull}
2018-09-04 11:18:59 +00:00
2021-07-19 14:33:52 +00:00
Results in an equivalent non-`Nullable` value for a [Nullable ](../../sql-reference/data-types/nullable.md ) type. In case the original value is `NULL` the result is undetermined. See also `ifNull` and `coalesce` functions.
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
assumeNotNull(x)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- `x` — The original value.
2018-09-04 11:18:59 +00:00
**Returned values**
2020-03-21 04:11:51 +00:00
- The original value from the non-`Nullable` type, if it is not `NULL` .
2021-05-03 10:39:10 +00:00
- Implementation specific result if the original value was `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
Consider the `t_null` table.
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SHOW CREATE TABLE t_null;
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─statement─────────────────────────────────────────────────────────────────┐
│ CREATE TABLE default.t_null ( x Int8, y Nullable(Int8)) ENGINE = TinyLog │
└───────────────────────────────────────────────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
```
2019-08-19 09:57:53 +00:00
Apply the `assumeNotNull` function to the `y` column.
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT assumeNotNull(y) FROM t_null;
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─assumeNotNull(y)─┐
│ 0 │
│ 3 │
└──────────────────┘
```
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT toTypeName(assumeNotNull(y)) FROM t_null;
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─toTypeName(assumeNotNull(y))─┐
│ Int8 │
│ Int8 │
└──────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## toNullable {#tonullable}
2018-09-04 11:18:59 +00:00
Converts the argument type to `Nullable` .
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
toNullable(x)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2020-03-21 04:11:51 +00:00
- `x` — The value of any non-compound type.
2018-09-04 11:18:59 +00:00
**Returned value**
2020-03-21 04:11:51 +00:00
- The input value with a `Nullable` type.
2018-09-04 11:18:59 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT toTypeName(10);
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─toTypeName(10)─┐
│ UInt8 │
└────────────────┘
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT toTypeName(toNullable(10));
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─toTypeName(toNullable(10))─┐
│ Nullable(UInt8) │
└────────────────────────────┘
```