> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for functions for working with nullable values

# Functions for working with nullable values

{/*AUTOGENERATED_START*/}

<h2 id="assumeNotNull">
  assumeNotNull
</h2>

Introduced in: v1.1.0

Returns the corresponding non-`Nullable` value for a value of type [`Nullable`](/reference/data-types/nullable).
If the original value is `NULL`, an arbitrary result can be returned.

See also: functions [`ifNull`](#ifNull) and [`coalesce`](#coalesce).

**Syntax**

```sql theme={null}
assumeNotNull(x)
```

**Arguments**

* `x` — The original value of any nullable type. [`Nullable(T)`](/reference/data-types/nullable)

**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`](/reference/data-types/index)

**Examples**

**Usage example**

```sql title=Query theme={null}
CREATE TABLE t_null (x Int8, y Nullable(Int8))
ENGINE=MergeTree()
ORDER BY x;

INSERT INTO t_null VALUES (1, NULL), (2, 3);

SELECT assumeNotNull(y) FROM table;
SELECT toTypeName(assumeNotNull(y)) FROM t_null;
```

```response title=Response theme={null}
┌─assumeNotNull(y)─┐
│                0 │
│                3 │
└──────────────────┘
┌─toTypeName(assumeNotNull(y))─┐
│ Int8                         │
│ Int8                         │
└──────────────────────────────┘
```

<h2 id="coalesce">
  coalesce
</h2>

Introduced in: v1.1.0

Returns the leftmost non-`NULL` argument.

**Syntax**

```sql theme={null}
coalesce(x[, y, ...])
```

**Arguments**

* `x[, y, ...]` — Any number of parameters of non-compound type. All parameters must be of mutually compatible data types. [`Any`](/reference/data-types/index)

**Returned value**

Returns the first non-`NULL` argument, otherwise `NULL`, if all arguments are `NULL`. [`Any`](/reference/data-types/index) or [`NULL`](/reference/syntax#null)

**Examples**

**Usage example**

```sql title=Query theme={null}
-- Consider a list of contacts that may specify multiple ways to contact a customer.

CREATE TABLE aBook
(
    name String,
    mail Nullable(String),
    phone Nullable(String),
    telegram Nullable(UInt32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO aBook VALUES ('client 1', NULL, '123-45-67', 123), ('client 2', NULL, NULL, NULL);

-- 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

SELECT name, coalesce(mail, phone, CAST(telegram,'Nullable(String)')) FROM aBook;
```

```response title=Response theme={null}
┌─name─────┬─coalesce(mail, phone, CAST(telegram, 'Nullable(String)'))─┐
│ client 1 │ 123-45-67                                                 │
│ client 2 │ ᴺᵁᴸᴸ                                                      │
└──────────┴───────────────────────────────────────────────────────────┘
```

<h2 id="firstNonDefault">
  firstNonDefault
</h2>

Introduced in: v25.9.0

Returns the first non-default value from a set of arguments

**Syntax**

```sql theme={null}
firstNonDefault(arg1[, arg2[ ...]])
```

**Arguments**

* `arg1` — The first argument to check - `arg2` — The second argument to check - `...` — Additional arguments to check

**Returned value**

Result type is the supertype of all arguments

**Examples**

**integers**

```sql title=Query theme={null}
SELECT firstNonDefault(0, 1, 2)
```

```response title=Response theme={null}
1
```

**strings**

```sql title=Query theme={null}
SELECT firstNonDefault('', 'hello', 'world')
```

```response title=Response theme={null}
'hello'
```

**nulls**

```sql title=Query theme={null}
SELECT firstNonDefault(NULL, 0 :: UInt8, 1 :: UInt8)
```

```response title=Response theme={null}
1
```

**nullable zero**

```sql title=Query theme={null}
SELECT firstNonDefault(NULL, 0 :: Nullable(UInt8), 1 :: Nullable(UInt8))
```

```response title=Response theme={null}
0
```

<h2 id="ifNull">
  ifNull
</h2>

Introduced in: v1.1.0

Returns an alternative value if the first argument is `NULL`.

**Syntax**

```sql theme={null}
ifNull(x, alt)
```

**Arguments**

* `x` — The value to check for `NULL`. [`Any`](/reference/data-types/index)
* `alt` — The value that the function returns if `x` is `NULL`. [`Any`](/reference/data-types/index)

**Returned value**

Returns the value of `x` if it is not `NULL`, otherwise `alt`. [`Any`](/reference/data-types/index)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT ifNull('a', 'b'), ifNull(NULL, 'b');
```

```response title=Response theme={null}
┌─ifNull('a', 'b')─┬─ifNull(NULL, 'b')─┐
│ a                │ b                 │
└──────────────────┴───────────────────┘
```

<h2 id="isNotNull">
  isNotNull
</h2>

Introduced in: v1.1.0

Checks if the argument is not `NULL`.

Also see: operator [`IS NOT NULL`](/reference/operators/index#is_not_null).

**Syntax**

```sql theme={null}
isNotNull(x)
```

**Arguments**

* `x` — A value of non-compound data type. [`Any`](/reference/data-types/index)

**Returned value**

Returns `1` if `x` is not `NULL`, otherwise `0`. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
CREATE TABLE t_null
(
  x Int32,
  y Nullable(Int32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO t_null VALUES (1, NULL), (2, 3);

SELECT x FROM t_null WHERE isNotNull(y);
```

```response title=Response theme={null}
┌─x─┐
│ 2 │
└───┘
```

<h2 id="isNull">
  isNull
</h2>

Introduced in: v1.1.0

Checks if the argument is `NULL`.

Also see: operator [`IS NULL`](/reference/operators/index#is_null).

**Syntax**

```sql theme={null}
isNull(x)
```

**Arguments**

* `x` — A value of non-compound data type. [`Any`](/reference/data-types/index)

**Returned value**

Returns `1` if `x` is `NULL`, otherwise `0`. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
CREATE TABLE t_null
(
  x Int32,
  y Nullable(Int32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO t_null VALUES (1, NULL), (2, 3);

SELECT x FROM t_null WHERE isNull(y);
```

```response title=Response theme={null}
┌─x─┐
│ 1 │
└───┘
```

<h2 id="isNullable">
  isNullable
</h2>

Introduced in: v22.7.0

Checks whether the argument's data type is `Nullable` (i.e it allows `NULL` values).

**Syntax**

```sql theme={null}
isNullable(x)
```

**Arguments**

* `x` — A value of any data type. [`Any`](/reference/data-types/index)

**Returned value**

Returns `1` if `x` is of a `Nullable` data type, otherwise `0`. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
CREATE TABLE tab (
    ordinary_col UInt32,
    nullable_col Nullable(UInt32)
)
ENGINE = MergeTree
ORDER BY tuple();
INSERT INTO tab (ordinary_col, nullable_col) VALUES (1,1), (2, 2), (3,3);
SELECT isNullable(ordinary_col), isNullable(nullable_col) FROM tab;
```

```response title=Response theme={null}
┌───isNullable(ordinary_col)──┬───isNullable(nullable_col)──┐
│                           0 │                           1 │
│                           0 │                           1 │
│                           0 │                           1 │
└─────────────────────────────┴─────────────────────────────┘
```

<h2 id="isZeroOrNull">
  isZeroOrNull
</h2>

Introduced in: v20.3.0

Checks if the argument is either zero (`0`) or `NULL`.

**Syntax**

```sql theme={null}
isZeroOrNull(x)
```

**Arguments**

* `x` — A numeric value. [`UInt`](/reference/data-types/int-uint)

**Returned value**

Returns `1` if `x` is `NULL` or equal to zero, otherwise `0`. [`UInt8/16/32/64`](/reference/data-types/int-uint) or [`Float32/Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
CREATE TABLE t_null
(
  x Int32,
  y Nullable(Int32)
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO t_null VALUES (1, NULL), (2, 0), (3, 3);

SELECT x FROM t_null WHERE isZeroOrNull(y);
```

```response title=Response theme={null}
┌─x─┐
│ 1 │
│ 2 │
└───┘
```

<h2 id="nullIf">
  nullIf
</h2>

Introduced in: v1.1.0

Returns `NULL` if both arguments are equal.

**Syntax**

```sql theme={null}
nullIf(x, y)
```

**Arguments**

* `x` — The first value. [`Any`](/reference/data-types/index)
* `y` — The second value. [`Any`](/reference/data-types/index)

**Returned value**

Returns `NULL` if both arguments are equal, otherwise returns the first argument. [`NULL`](/reference/syntax#null) or [`Nullable(x)`](/reference/data-types/nullable)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT nullIf(1, 1), nullIf(1, 2);
```

```response title=Response theme={null}
┌─nullIf(1, 1)─┬─nullIf(1, 2)─┐
│         ᴺᵁᴸᴸ │            1 │
└──────────────┴──────────────┘
```

<h2 id="toNullable">
  toNullable
</h2>

Introduced in: v1.1.0

Converts the provided argument type to `Nullable`.

**Syntax**

```sql theme={null}
toNullable(x)
```

**Arguments**

* `x` — A value of any non-compound type. [`Any`](/reference/data-types/index)

**Returned value**

Returns the input value but of `Nullable` type. [`Nullable(Any)`](/reference/data-types/nullable)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT toTypeName(10), toTypeName(toNullable(10));
```

```response title=Response theme={null}
┌─toTypeName(10)─┬─toTypeName(toNullable(10))─┐
│ UInt8          │ Nullable(UInt8)            │
└────────────────┴────────────────────────────┘
```
