2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/functions-for-nulls
2023-04-19 17:05:55 +00:00
sidebar_position: 135
2022-04-09 13:29:05 +00:00
sidebar_label: Nullable
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Functions for Working with Nullable Values
2018-09-04 11:18:59 +00:00
2022-06-02 10:55:18 +00:00
## isNull
2018-09-04 11:18:59 +00:00
2023-06-28 20:42:01 +00:00
Returns whether the argument is [NULL ](../../sql-reference/syntax.md#null ).
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
2023-06-01 18:27:34 +00:00
- `x` — A value of non-compound data type.
2018-09-04 11:18:59 +00:00
**Returned value**
2023-04-19 15:55:29 +00:00
- `1` if `x` is `NULL` .
- `0` if `x` is not `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
2023-06-01 18:27:34 +00:00
Table:
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─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
```
2023-06-01 18:27:34 +00:00
Query:
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 x FROM t_null WHERE isNull(y);
2018-09-04 11:18:59 +00:00
```
2020-03-20 10:10:48 +00:00
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┐
│ 1 │
└───┘
```
2022-06-02 10:55:18 +00:00
## isNotNull
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
Returns whether the argument is not [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
2023-06-01 18:27:34 +00:00
- `x` — A value of non-compound data type.
2018-09-04 11:18:59 +00:00
**Returned value**
2023-04-19 15:55:29 +00:00
- `1` if `x` is not `NULL` .
2023-06-01 18:27:34 +00:00
- `0` if `x` is `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
2023-06-01 18:27:34 +00:00
Table:
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─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
```
2023-06-01 18:27:34 +00:00
Query:
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 x FROM t_null WHERE isNotNull(y);
2018-09-04 11:18:59 +00:00
```
2020-03-20 10:10:48 +00:00
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┐
│ 2 │
└───┘
```
2022-06-02 10:55:18 +00:00
## coalesce
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
Returns the leftmost non-`NULL` argument.
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
coalesce(x,...)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
- Any number of parameters of non-compound type. All parameters must be of mutually compatible data types.
2018-09-04 11:18:59 +00:00
**Returned values**
2023-06-01 18:27:34 +00:00
- The first non-`NULL` argument
2023-04-19 15:55:29 +00:00
- `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
2023-06-01 18:27:34 +00:00
┌─name─────┬─mail─┬─phone─────┬──telegram─┐
│ client 1 │ ᴺᵁᴸᴸ │ 123-45-67 │ 123 │
│ client 2 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
└──────────┴──────┴───────────┴───────────┘
2018-09-04 11:18:59 +00:00
```
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
2023-06-01 18:27:34 +00:00
SELECT name, coalesce(mail, phone, CAST(telegram,'Nullable(String)')) FROM aBook;
2018-09-04 11:18:59 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2023-06-01 18:27:34 +00:00
┌─name─────┬─coalesce(mail, phone, CAST(telegram, 'Nullable(String)'))─┐
│ client 1 │ 123-45-67 │
│ client 2 │ ᴺᵁᴸᴸ │
└──────────┴───────────────────────────────────────────────────────────┘
2018-09-04 11:18:59 +00:00
```
2022-06-02 10:55:18 +00:00
## ifNull
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
Returns an alternative value if the argument is `NULL` .
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2023-06-01 18:27:34 +00:00
ifNull(x, alt)
2018-09-04 11:18:59 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2023-04-19 15:55:29 +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**
2023-06-01 18:27:34 +00:00
- `x` if `x` is not `NULL` .
- `alt` if `x` is `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─ifNull('a', 'b')─┐
│ a │
└──────────────────┘
```
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─ifNull(NULL, 'b')─┐
│ b │
└───────────────────┘
```
2022-06-02 10:55:18 +00:00
## nullIf
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
Returns `NULL` if both arguments are equal.
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
nullIf(x, y)
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
`x` , `y` — Values to compare. Must be of compatible types.
2018-09-04 11:18:59 +00:00
**Returned values**
2023-06-01 18:27:34 +00:00
- `NULL` if the arguments are equal.
- `x` if the arguments are not equal.
2018-09-04 11:18:59 +00:00
**Example**
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─nullIf(1, 1)─┐
│ ᴺᵁᴸᴸ │
└──────────────┘
```
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─nullIf(1, 2)─┐
│ 1 │
└──────────────┘
```
2022-06-02 10:55:18 +00:00
## assumeNotNull
2018-09-04 11:18:59 +00:00
2023-06-01 18:27:34 +00:00
Returns the corresponding non-`Nullable` value for a value of [Nullable ](../../sql-reference/data-types/nullable.md ) type. If the original value is `NULL` , an arbitrary result can be returned. See also functions `ifNull` and `coalesce` .
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
2023-04-19 15:55:29 +00:00
- `x` — The original value.
2018-09-04 11:18:59 +00:00
**Returned values**
2023-06-01 18:27:34 +00:00
- The input value as non-`Nullable` type, if it is not `NULL` .
2023-06-02 11:14:52 +00:00
- An arbitrary value, if the input value is `NULL` .
2018-09-04 11:18:59 +00:00
**Example**
2023-06-01 18:27:34 +00:00
Table:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
```
2023-06-01 18:27:34 +00:00
Query:
2018-09-04 11:18:59 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2023-06-01 18:27:34 +00:00
SELECT assumeNotNull(y) FROM table;
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─assumeNotNull(y)─┐
│ 0 │
│ 3 │
└──────────────────┘
```
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─toTypeName(assumeNotNull(y))─┐
│ Int8 │
│ Int8 │
└──────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## 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
2023-06-01 18:27:34 +00:00
- `x` — A value of non-compound type.
2018-09-04 11:18:59 +00:00
**Returned value**
2023-06-01 18:27:34 +00:00
- The input value but of `Nullable` type.
2018-09-04 11:18:59 +00:00
**Example**
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
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
2023-06-01 18:27:34 +00:00
Query:
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
2023-06-01 18:27:34 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─toTypeName(toNullable(10))─┐
│ Nullable(UInt8) │
└────────────────────────────┘
```