Type conversion functions

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

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

**Examples**

**Usage example**

```
SELECT toInt256OrZero('123')
```

```
123
```

**Failed conversion returns zero**

```
SELECT toInt256OrZero('abc')
```

```
0
```

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

Introduced in: v1.1.0

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

Note

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

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:

- [`toInt32OrZero`](#toInt32OrZero).
- [`toInt32OrNull`](#toInt32OrNull).
- [`toInt32OrDefault`](#toInt32OrDefault).

**Syntax**

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

**Examples**

**Usage example**

```
SELECT
    toInt32(-32),
    toInt32(-32.32),
    toInt32('-32')
FORMAT Vertical
```

```
Row 1:
──────
toInt32(-32):    -32
toInt32(-32.32): -32
toInt32('-32'):  -32
```

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

Introduced in: v21.11.0

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

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

**Returned value**

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

**Examples**

**Successful conversion**

```
SELECT toInt32OrDefault('-32', CAST('-1', 'Int32'))
```

```
-32
```

**Failed conversion**

```
SELECT toInt32OrDefault('abc', CAST('-1', 'Int32'))
```

```
-1
```

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

Introduced in: v1.1.0

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

Note

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

See also:

- [`toInt32`](#toInt32).
- [`toInt32OrZero`](#toInt32OrZero).
- [`toInt32OrDefault`](#toInt32OrDefault).

**Syntax**

```
toInt32OrNull(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    toInt32OrNull('-32'),
    toInt32OrNull('abc')
FORMAT Vertical
```

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

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

Introduced in: v1.1.0

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

Note

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

See also:

- [`toInt32`](#toInt32).
- [`toInt32OrNull`](#toInt32OrNull).
- [`toInt32OrDefault`](#toInt32OrDefault).

**Syntax**

```
toInt32OrZero(x)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

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

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

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

Introduced in: v1.1.0

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

Note

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

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:

- [`toInt64OrZero`](#toInt64OrZero).
- [`toInt64OrNull`](#toInt64OrNull).
- [`toInt64OrDefault`](#toInt64OrDefault).

**Syntax**

```
toInt64(expr)
```

**Arguments**