Type conversion functions

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

**Returned value**

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

**Examples**

**Successful conversion**

```
SELECT toInt16OrDefault('-16', CAST('-1', 'Int16'))
```

```
-16
```

**Failed conversion**

```
SELECT toInt16OrDefault('abc', CAST('-1', 'Int16'))
```

```
-1
```

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

Introduced in: v1.1.0

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

Note

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

See also:

- [`toInt16`](#toInt16).
- [`toInt16OrZero`](#toInt16OrZero).
- [`toInt16OrDefault`](#toInt16OrDefault).

**Syntax**

```
toInt16OrNull(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toInt16OrNull('-16'),
    toInt16OrNull('abc')
FORMAT Vertical
```

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

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

Introduced in: v1.1.0

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

Note

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

See also:

- [`toInt16`](#toInt16).
- [`toInt16OrNull`](#toInt16OrNull).
- [`toInt16OrDefault`](#toInt16OrDefault).

**Syntax**

```
toInt16OrZero(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

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

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

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

Introduced in: v1.1.0

Converts an input value to a value of type [Int256](/docs/sql-reference/data-types/int-uint).
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 toInt256('0xc0fe');`.

Note

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

See also:

- [`toInt256OrZero`](#toInt256OrZero).
- [`toInt256OrNull`](#toInt256OrNull).
- [`toInt256OrDefault`](#toInt256OrDefault).

**Syntax**

```
toInt256(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 integer value. [`Int256`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT
    toInt256(-256),
    toInt256(-256.256),
    toInt256('-256')
FORMAT Vertical
```

```
Row 1:
──────
toInt256(-256):     -256
toInt256(-256.256): -256
toInt256('-256'):   -256
```

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

Introduced in: v21.11.0

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

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

**Returned value**

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

**Examples**

**Successful conversion**

```
SELECT toInt256OrDefault('-256', CAST('-1', 'Int256'))
```

```
-256
```

**Failed conversion**

```
SELECT toInt256OrDefault('abc', CAST('-1', 'Int256'))
```

```
-1
```

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

Introduced in: v20.8.0

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

Note

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

See also:

- [`toInt256`](#toInt256).
- [`toInt256OrZero`](#toInt256OrZero).
- [`toInt256OrDefault`](#toInt256OrDefault).

**Syntax**

```
toInt256OrNull(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toInt256OrNull('-256'),
    toInt256OrNull('abc')
FORMAT Vertical
```

```
Row 1:
──────
toInt256OrNull('-256'): -256
toInt256OrNull('abc'):  \N
```

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

Introduced in: v20.8.0

Converts an input value to type [Int256](/docs/sql-reference/data-types/int-uint) but returns `0` in case of an error.
Like [`toInt256`](#toInt256) but returns `0` instead of throwing an exception.

See also:

- [`toInt256`](#toInt256).
- [`toInt256OrNull`](#toInt256OrNull).
- [`toInt256OrDefault`](#toInt256OrDefault).

**Syntax**

```
toInt256OrZero(x)
```

**Arguments**