Type conversion functions

**Examples**

**Usage example**

```
SELECT
    toFloat64OrZero('42.7'),
    toFloat64OrZero('abc')
FORMAT Vertical
```

```
Row 1:
──────
toFloat64OrZero('42.7'): 42.7
toFloat64OrZero('abc'):  0
```

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

Introduced in: v1.1.0

Converts an input value to a value of type [Int128](/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 toInt128('0xc0fe');`.

Note

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

See also:

- [`toInt128OrZero`](#toInt128OrZero).
- [`toInt128OrNull`](#toInt128OrNull).
- [`toInt128OrDefault`](#toInt128OrDefault).

**Syntax**

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

**Examples**

**Usage example**

```
SELECT
    toInt128(-128),
    toInt128(-128.8),
    toInt128('-128')
FORMAT Vertical
```

```
Row 1:
──────
toInt128(-128):   -128
toInt128(-128.8): -128
toInt128('-128'): -128
```

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

Introduced in: v21.11.0

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

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

**Returned value**

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

**Examples**

**Successful conversion**

```
SELECT toInt128OrDefault('-128', CAST('-1', 'Int128'))
```

```
-128
```

**Failed conversion**

```
SELECT toInt128OrDefault('abc', CAST('-1', 'Int128'))
```

```
-1
```

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

Introduced in: v20.8.0

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

Note

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

See also:

- [`toInt128`](#toInt128).
- [`toInt128OrZero`](#toInt128OrZero).
- [`toInt128OrDefault`](#toInt128OrDefault).

**Syntax**

```
toInt128OrNull(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toInt128OrNull('-128'),
    toInt128OrNull('abc')
FORMAT Vertical
```

```
Row 1:
──────
toInt128OrNull('-128'): -128
toInt128OrNull('abc'):  \N
```

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

Introduced in: v20.8.0

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

See also:

- [`toInt128`](#toInt128).
- [`toInt128OrNull`](#toInt128OrNull).
- [`toInt128OrDefault`](#toInt128OrDefault).

**Syntax**

```
toInt128OrZero(x)
```

**Arguments**

- `x` — Input value to convert. [`String`](/docs/sql-reference/data-types/string) or [`FixedString`](/docs/sql-reference/data-types/fixedstring) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal) or [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Date`](/docs/sql-reference/data-types/date) or [`DateTime`](/docs/sql-reference/data-types/datetime)

**Returned value**

Returns the converted input value, otherwise `0` if conversion fails. [`Int128`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT toInt128OrZero('123')
```

```
123
```

**Failed conversion returns zero**

```
SELECT toInt128OrZero('abc')
```

```
0
```

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

Introduced in: v1.1.0

Converts an input value to a value of type [`Int16`](/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 toInt16('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.
For example: `SELECT toInt16(32768) == -32768;`.

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:

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

**Syntax**

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

**Examples**

**Usage example**

```
SELECT
    toInt16(-16),
    toInt16(-16.16),
    toInt16('-16')
FORMAT Vertical
```

```
Row 1:
──────
toInt16(-16):    -16
toInt16(-16.16): -16
toInt16('-16'):  -16
```

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

Introduced in: v21.11.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 the default value in case of an error.
If no `default` value is passed then `0` is returned in case of an error.

**Syntax**

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