Type conversion functions

See also:

- [`toUInt128`](#toUInt128).
- [`toUInt128OrNull`](#toUInt128OrNull).
- [`toUInt128OrDefault`](#toUInt128OrDefault).

**Syntax**

```
toUInt128OrZero(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toUInt128OrZero('128'),
    toUInt128OrZero('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt128OrZero('128'): 128
toUInt128OrZero('abc'): 0
```

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

Introduced in: v1.1.0

Converts an input value to a value of type [`UInt16`](/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 arguments:

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

Note

If the input value cannot be represented within the bounds of [`UInt16`](/docs/sql-reference/data-types/int-uint), overflow or underflow of the result occurs.
This is not considered an error.
For example: `SELECT toUInt16(65536) == 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:

- [`toUInt16OrZero`](#toUInt16OrZero).
- [`toUInt16OrNull`](#toUInt16OrNull).
- [`toUInt16OrDefault`](#toUInt16OrDefault).

**Syntax**

```
toUInt16(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 16-bit unsigned integer value. [`UInt16`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT
    toUInt16(16),
    toUInt16(16.16),
    toUInt16('16')
FORMAT Vertical
```

```
Row 1:
──────
toUInt16(16):    16
toUInt16(16.16): 16
toUInt16('16'):  16
```

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

Introduced in: v21.11.0

Like [`toUInt16`](#toUInt16), this function converts an input value to a value of type [UInt16](/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**

```
toUInt16OrDefault(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. [`UInt16`](/docs/sql-reference/data-types/int-uint)

**Returned value**

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

**Examples**

**Successful conversion**

```
SELECT toUInt16OrDefault('16', CAST('0', 'UInt16'))
```

```
16
```

**Failed conversion**

```
SELECT toUInt16OrDefault('abc', CAST('0', 'UInt16'))
```

```
0
```

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

Introduced in: v1.1.0

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

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 toUInt16OrNull('0xc0fe');`.

Note

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

See also:

- [`toUInt16`](#toUInt16).
- [`toUInt16OrZero`](#toUInt16OrZero).
- [`toUInt16OrDefault`](#toUInt16OrDefault).

**Syntax**

```
toUInt16OrNull(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toUInt16OrNull('16'),
    toUInt16OrNull('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt16OrNull('16'):  16
toUInt16OrNull('abc'): \N
```

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

Introduced in: v1.1.0

Like [`toUInt16`](#toUInt16), this function converts an input value to a value of type [`UInt16`](/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 toUInt16OrZero('0xc0fe');`.

Note

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

See also:

- [`toUInt16`](#toUInt16).
- [`toUInt16OrNull`](#toUInt16OrNull).
- [`toUInt16OrDefault`](#toUInt16OrDefault).

**Syntax**

```
toUInt16OrZero(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toUInt16OrZero('16'),
    toUInt16OrZero('abc')
FORMAT Vertical
```

```
Row 1:
──────
toUInt16OrZero('16'):  16
toUInt16OrZero('abc'): 0
```

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

Introduced in: v1.1.0

Converts an input value to a value of type UInt256.
Throws an exception in case of an error.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Supported arguments:

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

Unsupported arguments:

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

Note

If the input value cannot be represented within the bounds of UInt256, the result over or under flows.
This is not considered an error.

See also:

- [`toUInt256OrZero`](#toUInt256OrZero).
- [`toUInt256OrNull`](#toUInt256OrNull).
- [`toUInt256OrDefault`](#toUInt256OrDefault).

**Syntax**

```
toUInt256(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 256-bit unsigned integer value. [`UInt256`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT
    toUInt256(256),
    toUInt256(256.256),
    toUInt256('256')
FORMAT Vertical
```

```
Row 1:
──────
toUInt256(256):     256
toUInt256(256.256): 256
toUInt256('256'):   256
```

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

Introduced in: v21.11.0

Like [`toUInt256`](#toUInt256), this function converts an input value to a value of type [UInt256](/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**

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

**Arguments**