2020-04-03 13:23:32 +00:00
---
2024-05-09 08:29:35 +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
2024-03-13 13:55:32 +00:00
See also operator [`IS NULL` ](../operators/index.md#is_null ).
2024-05-08 10:51:22 +00:00
**Syntax**
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
isNull(x)
```
2024-03-13 13:57:55 +00:00
Alias: `ISNULL` .
2021-02-16 10:53:44 +00:00
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 │
└───┘
```
2024-05-08 10:51:22 +00:00
## isNullable
2024-05-09 08:29:35 +00:00
Returns `1` if a column is [Nullable ](../data-types/nullable.md ) (i.e allows `NULL` values), `0` otherwise.
2024-05-08 10:51:22 +00:00
**Syntax**
``` sql
isNullable(x)
```
**Arguments**
- `x` — column.
**Returned value**
- `1` if `x` allows `NULL` values. [UInt8 ](../data-types/int-uint.md ).
- `0` if `x` does not allow `NULL` values. [UInt8 ](../data-types/int-uint.md ).
**Example**
Query:
``` sql
2024-05-09 08:29:35 +00:00
CREATE TABLE tab (ordinary_col UInt32, nullable_col Nullable(UInt32)) ENGINE = Log;
INSERT INTO tab (ordinary_col, nullable_col) VALUES (1,1), (2, 2), (3,3);
SELECT isNullable(ordinary_col), isNullable(nullable_col) FROM tab;
2024-05-08 10:51:22 +00:00
```
Result:
``` text
2024-05-09 08:29:35 +00:00
┌───isNullable(ordinary_col)──┬───isNullable(nullable_col)──┐
1. │ 0 │ 1 │
2. │ 0 │ 1 │
3. │ 0 │ 1 │
2024-05-08 10:51:22 +00:00
└─────────────────────────────┴─────────────────────────────┘
```
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
2024-03-13 13:55:32 +00:00
See also operator [`IS NOT NULL` ](../operators/index.md#is_not_null ).
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 │
└───┘
```
2024-05-08 11:56:49 +00:00
## 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` .
:::note
2024-05-09 08:29:35 +00:00
This function is an internal function used by the implementation of JOIN ON. Please do not use it manually in queries.
2024-05-08 11:56:49 +00:00
:::
**Syntax**
``` sql
isNotDistinctFrom(x, y)
```
**Arguments**
- `x` — first JOIN key.
- `y` — second JOIN key.
**Returned value**
- `true` when `x` and `y` are both `NULL` .
- `false` otherwise.
**Example**
For a complete example see: [NULL values in JOIN keys ](../../sql-reference/statements/select/join#null-values-in-join-keys ).
2023-09-05 17:31:37 +00:00
## isZeroOrNull
Returns whether the argument is 0 (zero) or [NULL ](../../sql-reference/syntax.md#null-literal ).
``` sql
isZeroOrNull(x)
```
**Arguments:**
- `x` — A value of non-compound data type.
**Returned value**
- `1` if `x` is 0 (zero) or `NULL` .
- `0` else.
**Example**
Table:
``` text
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 0 │
│ 3 │ 3 │
└───┴──────┘
```
Query:
``` sql
SELECT x FROM t_null WHERE isZeroOrNull(y);
```
Result:
``` text
┌─x─┐
│ 1 │
│ 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
```
2023-11-28 09:20:02 +00:00
The `mail` and `phone` fields are of type String, but the `telegram` field is `UInt32` , so it needs to be converted to `String` .
2018-09-04 11:18:59 +00:00
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
2024-05-24 03:54:16 +00:00
Returns the corresponding non-`Nullable` value for a value of [Nullable ](../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) │
└────────────────────────────┘
```