Type conversion functions

Supported arguments:

- String representations of (U)Int8/16/32/128/256.

Unsupported arguments (return `NULL`):

- String representations of Float\* values, including `NaN` and `Inf`.
- String representations of binary and hexadecimal values, e.g. `SELECT toUInt32OrNull('0xc0fe');`.

Note

If the input value cannot be represented within the bounds of [`UInt32`](/docs/sql-reference/data-types/int-uint), overflow or underflow of the result occurs.
This is not considered an error.

See also:

- [`toUInt32`](#toUInt32).
- [`toUInt32OrZero`](#toUInt32OrZero).
- [`toUInt32OrDefault`](#toUInt32OrDefault).

**Syntax**

```
toUInt32OrNull(x)
```

**Arguments**

- `x` — A String representation of a number. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a value of type `UInt32`, otherwise `NULL` if the conversion is unsuccessful. [`UInt32`](/docs/sql-reference/data-types/int-uint) or [`NULL`](/docs/sql-reference/syntax#null)

**Examples**

**Usage example**

```
SELECT
    toUInt32OrNull('32'),
    toUInt32OrNull('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt32OrNull('32'):  32
toUInt32OrNull('abc'): \N
```

## toUInt32OrZero[​](#toUInt32OrZero "Direct link to toUInt32OrZero")

Introduced in: v1.1.0

Like [`toUInt32`](#toUInt32), this function converts an input value to a value of type [`UInt32`](/docs/sql-reference/data-types/int-uint) but returns `0` in case of an error.

Supported arguments:

- String representations of (U)Int8/16/32/128/256.

Unsupported arguments (return `0`):

- String representations of Float\* values, including `NaN` and `Inf`.
- String representations of binary and hexadecimal values, e.g. `SELECT toUInt32OrZero('0xc0fe');`.

Note

If the input value cannot be represented within the bounds of [`UInt32`](/docs/sql-reference/data-types/int-uint), overflow or underflow of the result occurs.
This is not considered an error.

See also:

- [`toUInt32`](#toUInt32).
- [`toUInt32OrNull`](#toUInt32OrNull).
- [`toUInt32OrDefault`](#toUInt32OrDefault).

**Syntax**

```
toUInt32OrZero(x)
```

**Arguments**

- `x` — A String representation of a number. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a value of type UInt32, otherwise `0` if the conversion is unsuccessful. [`UInt32`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT
    toUInt32OrZero('32'),
    toUInt32OrZero('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt32OrZero('32'):  32
toUInt32OrZero('abc'): 0
```

## toUInt64[​](#toUInt64 "Direct link to toUInt64")

Introduced in: v1.1.0

Converts an input value to a value of type [`UInt64`](/docs/sql-reference/data-types/int-uint).
Throws an exception in case of an error.

Supported arguments:

- Values or string representations of type (U)Int\*.
- Values of type Float\*.

Unsupported types:

- String representations of Float\* values, including `NaN` and `Inf`.
- String representations of binary and hexadecimal values, e.g. `SELECT toUInt64('0xc0fe');`.

Note

If the input value cannot be represented within the bounds of [`UInt64`](/docs/sql-reference/data-types/int-uint), the result over or under flows.
This is not considered an error.
For example: `SELECT toUInt64(18446744073709551616) == 0;`

Note

The function uses [rounding towards zero](https://en.wikipedia.org/wiki/Rounding#Rounding_towards_zero), meaning it truncates fractional digits of numbers.

See also:

- [`toUInt64OrZero`](#toUInt64OrZero).
- [`toUInt64OrNull`](#toUInt64OrNull).
- [`toUInt64OrDefault`](#toUInt64OrDefault).

**Syntax**

```
toUInt64(expr)
```

**Arguments**

- `expr` — Expression returning a number or a string representation of a number. [`Expression`](/docs/sql-reference/data-types/special-data-types/expression)

**Returned value**

Returns a 64-bit unsigned integer value. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT
    toUInt64(64),
    toUInt64(64.64),
    toUInt64('64')
FORMAT Vertical
```

```
Row 1:
──────
toUInt64(64):    64
toUInt64(64.64): 64
toUInt64('64'):  64
```

## toUInt64OrDefault[​](#toUInt64OrDefault "Direct link to toUInt64OrDefault")

Introduced in: v21.11.0

Like [`toUInt64`](#toUInt64), this function converts an input value to a value of type [UInt64](/docs/sql-reference/data-types/int-uint) but returns the default value in case of an error.
If no `default` value is passed then `0` is returned in case of an error.

**Syntax**

```
toUInt64OrDefault(expr[, default])
```

**Arguments**

- `expr` — Expression returning a number or a string representation of a number. [`String`](/docs/sql-reference/data-types/string) or [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `default` — Optional. The default value to return if parsing is unsuccessful. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns a value of type UInt64 if successful, otherwise returns the default value if passed, or 0 if not. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Successful conversion**

```
SELECT toUInt64OrDefault('64', CAST('0', 'UInt64'))
```

```
64
```

**Failed conversion**

```
SELECT toUInt64OrDefault('abc', CAST('0', 'UInt64'))
```

```
0
```

## toUInt64OrNull[​](#toUInt64OrNull "Direct link to toUInt64OrNull")

Introduced in: v1.1.0

Like [`toUInt64`](#toUInt64), this function converts an input value to a value of type [`UInt64`](/docs/sql-reference/data-types/int-uint) but returns `NULL` in case of an error.

Supported arguments:

- String representations of (U)Int\*.

Unsupported arguments (return `NULL`):

- String representations of Float\* values, including `NaN` and `Inf`.
- String representations of binary and hexadecimal values, e.g. `SELECT toUInt64OrNull('0xc0fe');`.

Note

If the input value cannot be represented within the bounds of [`UInt64`](/docs/sql-reference/data-types/int-uint), overflow or underflow of the result occurs.
This is not considered an error.

See also:

- [`toUInt64`](#toUInt64).
- [`toUInt64OrZero`](#toUInt64OrZero).
- [`toUInt64OrDefault`](#toUInt64OrDefault).

**Syntax**

```
toUInt64OrNull(x)
```

**Arguments**

- `x` — A String representation of a number. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a value of type UInt64, otherwise `NULL` if the conversion is unsuccessful. [`UInt64`](/docs/sql-reference/data-types/int-uint) or [`NULL`](/docs/sql-reference/syntax#null)

**Examples**

**Usage example**

```
SELECT
    toUInt64OrNull('64'),
    toUInt64OrNull('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt64OrNull('64'):  64
toUInt64OrNull('abc'): \N
```

## toUInt64OrZero[​](#toUInt64OrZero "Direct link to toUInt64OrZero")

Introduced in: v1.1.0

Like [`toUInt64`](#toUInt64), this function converts an input value to a value of type [`UInt64`](/docs/sql-reference/data-types/int-uint) but returns `0` in case of an error.

Supported arguments:

- String representations of (U)Int\*.

Unsupported arguments (return `0`):

- String representations of Float\* values, including `NaN` and `Inf`.
- String representations of binary and hexadecimal values, e.g. `SELECT toUInt64OrZero('0xc0fe');`.

Note

If the input value cannot be represented within the bounds of [`UInt64`](/docs/sql-reference/data-types/int-uint), overflow or underflow of the result occurs.
This is not considered an error.

See also:

- [`toUInt64`](#toUInt64).
- [`toUInt64OrNull`](#toUInt64OrNull).
- [`toUInt64OrDefault`](#toUInt64OrDefault).

**Syntax**

```
toUInt64OrZero(x)
```

**Arguments**

- `x` — A String representation of a number. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a value of type UInt64, otherwise `0` if the conversion is unsuccessful. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT
    toUInt64OrZero('64'),
    toUInt64OrZero('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt64OrZero('64'):  64
toUInt64OrZero('abc'): 0
```

## toUInt8[​](#toUInt8 "Direct link to toUInt8")

Introduced in: v1.1.0