Type conversion functions

```
┌─reinterpretAsUUID(reverse(unhex('000102030405060708090a0b0c0d0e0f')))─┐
│                                  08090a0b-0c0d-0e0f-0001-020304050607 │
└───────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v1.1.0

Converts an input value to a value of type BFloat16.
Throws an exception in case of an error.

See also:

- [`toBFloat16OrZero`](#toBFloat16OrZero).
- [`toBFloat16OrNull`](#toBFloat16OrNull).

**Syntax**

```
toBFloat16(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 brain-float value. [`BFloat16`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT
toBFloat16(toFloat32(42.7)),
toBFloat16(toFloat32('42.7')),
toBFloat16('42.7')
FORMAT Vertical;
```

```
toBFloat16(toFloat32(42.7)): 42.5
toBFloat16(t⋯32('42.7')):    42.5
toBFloat16('42.7'):          42.5
```

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

Introduced in: v1.1.0

Converts a String input value to a value of type BFloat16.
If the string does not represent a floating point value, the function returns NULL.

Supported arguments:

- String representations of numeric values.

Unsupported arguments (return `NULL`):

- String representations of binary and hexadecimal values.
- Numeric values.

Note

The function allows a silent loss of precision while converting from the string representation.

See also:

- [`toBFloat16`](#toBFloat16).
- [`toBFloat16OrZero`](#toBFloat16OrZero).

**Syntax**

```
toBFloat16OrNull(x)
```

**Arguments**

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

**Returned value**

Reurns a 16-bit brain-float value, otherwise `NULL`. [`BFloat16`](/docs/sql-reference/data-types/float) or [`NULL`](/docs/sql-reference/syntax#null)

**Examples**

**Usage example**

```
SELECT toBFloat16OrNull('0x5E'), -- unsupported arguments
       toBFloat16OrNull('12.3'), -- typical use
       toBFloat16OrNull('12.3456789') -- silent loss of precision
```

```
\N
12.25
12.3125
```

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

Introduced in: v1.1.0

Converts a String input value to a value of type BFloat16.
If the string does not represent a floating point value, the function returns zero.

Supported arguments:

- String representations of numeric values.

Unsupported arguments (return `0`):

- String representations of binary and hexadecimal values.
- Numeric values.

Note

The function allows a silent loss of precision while converting from the string representation.

See also:

- [`toBFloat16`](#toBFloat16).
- [`toBFloat16OrNull`](#toBFloat16OrNull).

**Syntax**

```
toBFloat16OrZero(x)
```

**Arguments**

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

**Returned value**

Returns a 16-bit brain-float value, otherwise `0`. [`BFloat16`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT toBFloat16OrZero('0x5E'), -- unsupported arguments
       toBFloat16OrZero('12.3'), -- typical use
       toBFloat16OrZero('12.3456789') -- silent loss of precision
```

```
0
12.25
12.3125
```

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

Introduced in: v22.2.0

Converts an input value to a value of type Bool.

**Syntax**

```
toBool(expr)
```

**Arguments**

- `expr` — Expression returning a number or a string. For strings, accepts 'true' or 'false' (case-insensitive). [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`String`](/docs/sql-reference/data-types/string) or [`Expression`](/docs/sql-reference/data-types/special-data-types/expression)

**Returned value**

Returns `true` or `false` based on evaluation of the argument. [`Bool`](/docs/sql-reference/data-types/boolean)

**Examples**

**Usage example**

```
SELECT
    toBool(toUInt8(1)),
    toBool(toInt8(-1)),
    toBool(toFloat32(1.01)),
    toBool('true'),
    toBool('false'),
    toBool('FALSE')
FORMAT Vertical
```

```
toBool(toUInt8(1)):      true
toBool(toInt8(-1)):      true
toBool(toFloat32(1.01)): true
toBool('true'):          true
toBool('false'):         false
toBool('FALSE'):         false
```

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

Introduced in: v1.1.0

Converts an input value to type [`Date`](/docs/sql-reference/data-types/date).
Supports conversion from String, FixedString, DateTime, or numeric types.

**Syntax**

```
toDate(x)
```

**Arguments**

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

**Returned value**

Returns the converted input value. [`Date`](/docs/sql-reference/data-types/date)

**Examples**

**String to Date conversion**

```
SELECT toDate('2025-04-15')
```

```
2025-04-15
```

**DateTime to Date conversion**

```
SELECT toDate(toDateTime('2025-04-15 10:30:00'))
```

```
2025-04-15
```

**Integer to Date conversion**

```
SELECT toDate(20297)
```

```
2025-07-28
```

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

Introduced in: v21.9.0

Converts the argument to the [Date32](/docs/sql-reference/data-types/date32) data type.
If the value is outside the range, `toDate32` returns the border values supported by [Date32](/docs/sql-reference/data-types/date32).
If the argument is of type [`Date`](/docs/sql-reference/data-types/date), it's bounds are taken into account.

**Syntax**

```
toDate32(expr)
```

**Arguments**

- `expr` — The value to convert. [`String`](/docs/sql-reference/data-types/string) or [`UInt32`](/docs/sql-reference/data-types/int-uint) or [`Date`](/docs/sql-reference/data-types/date)

**Returned value**

Returns a calendar date. [`Date32`](/docs/sql-reference/data-types/date32)

**Examples**

**Within range**

```
SELECT toDate32('2025-01-01') AS value, toTypeName(value)
FORMAT Vertical
```

```
Row 1:
──────
value:           2025-01-01
toTypeName(value): Date32
```

**Outside range**

```
SELECT toDate32('1899-01-01') AS value, toTypeName(value)
FORMAT Vertical
```

```
Row 1:
──────
value:           1900-01-01
toTypeName(value): Date32
```

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

Introduced in: v21.11.0

Converts the argument to the [Date32](/docs/sql-reference/data-types/date32) data type. If the value is outside the range, `toDate32OrDefault` returns the lower border value supported by [Date32](/docs/sql-reference/data-types/date32). If the argument has [Date](/docs/sql-reference/data-types/date) type, it's borders are taken into account. Returns default value if an invalid argument is received.

**Syntax**

```
toDate32OrDefault(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. [`Date32`](/docs/sql-reference/data-types/date32)

**Returned value**

Value of type Date32 if successful, otherwise returns the default value if passed or 1900-01-01 if not. [`Date32`](/docs/sql-reference/data-types/date32)

**Examples**

**Successful conversion**

```
SELECT toDate32OrDefault('1930-01-01', toDate32('2020-01-01'))
```

```
1930-01-01
```

**Failed conversion**

```
SELECT toDate32OrDefault('xx1930-01-01', toDate32('2020-01-01'))
```

```
2020-01-01
```

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

Introduced in: v21.9.0