Type conversion functions

The following functions are available for type conversion.

Common issues with data conversion

to<type> functions and cast behave differently in some cases, for example in case of LowCardinality: cast removes LowCardinality trait to<type> functions don't. The same with Nullable, this behaviour isn't compatible with SQL standard, and it can be changed using cast_keep_nullable setting.

Be aware of potential data loss if values of a datatype are converted to a smaller datatype (for example from Int64 to Int32) or between incompatible datatypes (for example from String to Int). Make sure to check carefully if the result is as expected.

Example:

SELECT
    toTypeName(toLowCardinality('') AS val) AS source_type,
    toTypeName(toString(val)) AS to_type_result_type,
    toTypeName(CAST(val, 'String')) AS cast_result_type

┌─source_type────────────┬─to_type_result_type────┬─cast_result_type─┐
│ LowCardinality(String) │ LowCardinality(String) │ String           │
└────────────────────────┴────────────────────────┴──────────────────┘

SELECT
    toTypeName(toNullable('') AS val) AS source_type,
    toTypeName(toString(val)) AS to_type_result_type,
    toTypeName(CAST(val, 'String')) AS cast_result_type

┌─source_type──────┬─to_type_result_type─┬─cast_result_type─┐
│ Nullable(String) │ Nullable(String)    │ String           │
└──────────────────┴─────────────────────┴──────────────────┘

SELECT
    toTypeName(toNullable('') AS val) AS source_type,
    toTypeName(toString(val)) AS to_type_result_type,
    toTypeName(CAST(val, 'String')) AS cast_result_type
SETTINGS cast_keep_nullable = 1

┌─source_type──────┬─to_type_result_type─┬─cast_result_type─┐
│ Nullable(String) │ Nullable(String)    │ Nullable(String) │
└──────────────────┴─────────────────────┴──────────────────┘

toBool

Converts an input value to a boolean type. This function will throw an error if the conversion is not possible.

Syntax

toBool(expr)

Arguments

  • expr: An expression that evaluates to a number or a string.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string values: 'true' or 'false' (case-insensitive).

Returns

A boolean value (true or false). Bool.

Example

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

Result:

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

toInt8

Converts an input value to an 8-bit signed integer. This function will throw an error if the conversion is not possible.

Syntax

toInt8(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int8, overflow or underflow of the result occurs. This isn't considered an error. For example: SELECT toInt8(128) == -128;.

Returns

An 8-bit signed integer. Int8.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt8(-8),
    toInt8(-8.8),
    toInt8('-8')

Result:

Row 1:
──────
toInt8(-8):   -8
toInt8(-8.8): -8
toInt8('-8'): -8

toInt8OrZero

Converts an input value to an 8-bit signed integer, returning 0 if the conversion fails.

Syntax

toInt8OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int8, overflow or underflow of the result occurs. This isn't considered an error.

Returns

An 8-bit signed integer if successful, otherwise 0. Int8.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt8OrZero('-8'),
    toInt8OrZero('abc')

Result:

Row 1:
──────
toInt8OrZero('-8'):  -8
toInt8OrZero('abc'): 0

toInt8OrNull

Converts an input value to an 8-bit signed integer, returning NULL if the conversion fails.

Syntax

toInt8OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int8, overflow or underflow of the result occurs. This isn't considered an error.

Returns

An 8-bit signed integer if successful, otherwise NULL. Int8 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt8OrNull('-8'),
    toInt8OrNull('abc')

Result:

Row 1:
──────
toInt8OrNull('-8'):  -8
toInt8OrNull('abc'): ᴺᵁᴸᴸ

toInt8OrDefault

Converts an input value to an 8-bit signed integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toInt8OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The Int8 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of Int8, overflow or underflow of the result occurs. This isn't considered an error.

Returns

An 8-bit signed integer if successful, otherwise the default value (if provided) or 0. Int8.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toInt8OrDefault('-8', CAST('-1', 'Int8')),
    toInt8OrDefault('abc', CAST('-1', 'Int8'))

Result:

Row 1:
──────
toInt8OrDefault('-8', CAST('-1', 'Int8')):  -8
toInt8OrDefault('abc', CAST('-1', 'Int8')): -1

toInt16

Converts an input value to a 16-bit signed integer. This function will throw an error if the conversion is not possible.

Syntax

toInt16(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int16, overflow or underflow of the result occurs. This isn't considered an error. For example: SELECT toInt16(32768) == -32768;.

Returns

A 16-bit signed integer. Int16.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt16(-16),
    toInt16(-16.16),
    toInt16('-16')

Result:

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

toInt16OrZero

Converts an input value to a 16-bit signed integer, returning 0 if the conversion fails.

Syntax

toInt16OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int16, overflow or underflow of the result occurs. This isn't considered as an error.

Returns

A 16-bit signed integer if successful, otherwise 0. Int16.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt16OrZero('-16'),
    toInt16OrZero('abc')

Result:

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

toInt16OrNull

Converts an input value to a 16-bit signed integer, returning NULL if the conversion fails.

Syntax

toInt16OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int16, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 16-bit signed integer if successful, otherwise NULL. Int16 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt16OrNull('-16'),
    toInt16OrNull('abc')

Result:

Row 1:
──────
toInt16OrNull('-16'): -16
toInt16OrNull('abc'): ᴺᵁᴸᴸ

toInt16OrDefault

Converts an input value to a 16-bit signed integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toInt16OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The Int16 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of Int16, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 16-bit signed integer if successful, otherwise the default value (if provided) or 0. Int16.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

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

Result:

Row 1:
──────
toInt16OrDefault('-16', CAST('-1', 'Int16')): -16
toInt16OrDefault('abc', CAST('-1', 'Int16')): -1

toInt32

Converts an input value to a 32-bit signed integer. This function will throw an error if the conversion is not possible.

Syntax

toInt32(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int32, the result over or under flows. This isn't considered an error. For example: SELECT toInt32(2147483648) == -2147483648;

Returns

A 32-bit signed integer. Int32.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt32(-32),
    toInt32(-32.32),
    toInt32('-32')

Result:

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

toInt32OrZero

Converts an input value to a 32-bit signed integer, returning 0 if the conversion fails.

Syntax

toInt32OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int32, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 32-bit signed integer if successful, otherwise 0. Int32.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt32OrZero('-32'),
    toInt32OrZero('abc')

Result:

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

toInt32OrNull

Converts an input value to a 32-bit signed integer, returning NULL if the conversion fails.

Syntax

toInt32OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int32, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 32-bit signed integer if successful, otherwise NULL. Int32 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt32OrNull('-32'),
    toInt32OrNull('abc')

Result:

Row 1:
──────
toInt32OrNull('-32'): -32
toInt32OrNull('abc'): ᴺᵁᴸᴸ

toInt32OrDefault

Converts an input value to a 32-bit signed integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toInt32OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The Int32 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of Int32, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 32-bit signed integer if successful, otherwise the default value (if provided) or 0. Int32.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

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

Result:

Row 1:
──────
toInt32OrDefault('-32', CAST('-1', 'Int32')): -32
toInt32OrDefault('abc', CAST('-1', 'Int32')): -1

toInt64

Converts an input value to a 64-bit signed integer. This function will throw an error if the conversion is not possible.

Syntax

toInt64(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int64, the result over or under flows. This isn't considered an error. For example: SELECT toInt64(9223372036854775808) == -9223372036854775808;

Returns

A 64-bit signed integer. Int64.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt64(-64),
    toInt64(-64.64),
    toInt64('-64')

Result:

Row 1:
──────
toInt64(-64):    -64
toInt64(-64.64): -64
toInt64('-64'):  -64

toInt64OrZero

Converts an input value to a 64-bit signed integer, returning 0 if the conversion fails.

Syntax

toInt64OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int64, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 64-bit signed integer if successful, otherwise 0. Int64.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt64OrZero('-64'),
    toInt64OrZero('abc')

Result:

Row 1:
──────
toInt64OrZero('-64'): -64
toInt64OrZero('abc'): 0

toInt64OrNull

Converts an input value to a 64-bit signed integer, returning NULL if the conversion fails.

Syntax

toInt64OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int64, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 64-bit signed integer if successful, otherwise NULL. Int64 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt64OrNull('-64'),
    toInt64OrNull('abc')

Result:

Row 1:
──────
toInt64OrNull('-64'): -64
toInt64OrNull('abc'): ᴺᵁᴸᴸ

toInt64OrDefault

Converts an input value to a 64-bit signed integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toInt64OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The Int64 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of Int64, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 64-bit signed integer if successful, otherwise the default value (if provided) or 0. Int64.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toInt64OrDefault('-64', CAST('-1', 'Int64')),
    toInt64OrDefault('abc', CAST('-1', 'Int64'))

Result:

Row 1:
──────
toInt64OrDefault('-64', CAST('-1', 'Int64')): -64
toInt64OrDefault('abc', CAST('-1', 'Int64')): -1

toInt128

Converts an input value to a 128-bit signed integer. This function will throw an error if the conversion is not possible.

Syntax

toInt128(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int128, the result over or under flows. This isn't considered an error.

Returns

A 128-bit signed integer. Int128.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt128(-128),
    toInt128(-128.8),
    toInt128('-128')

Result:

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

toInt128OrZero

Converts an input value to a 128-bit signed integer, returning 0 if the conversion fails.

Syntax

toInt128OrZero(expr)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int128, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 128-bit signed integer if successful, otherwise 0. Int128.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt128OrZero('-128'),
    toInt128OrZero('abc')

Result:

Row 1:
──────
toInt128OrZero('-128'): -128
toInt128OrZero('abc'):  0

toInt128OrNull

Converts an input value to a 128-bit signed integer, returning NULL if the conversion fails.

Syntax

toInt128OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int128, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 128-bit signed integer if successful, otherwise NULL. Int128 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt128OrNull('-128'),
    toInt128OrNull('abc')

Result:

Row 1:
──────
toInt128OrNull('-128'): -128
toInt128OrNull('abc'):  ᴺᵁᴸᴸ

toInt128OrDefault

Converts an input value to a 128-bit signed integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toInt128OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The Int128 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of Int128, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 128-bit signed integer if successful, otherwise the default value (if provided) or 0. Int128.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

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

Result:

Row 1:
──────
toInt128OrDefault('-128', CAST('-1', 'Int128')): -128
toInt128OrDefault('abc', CAST('-1', 'Int128')):  -1

toInt256

Converts an input value to a 256-bit signed integer. This function will throw an error if the conversion is not possible.

Syntax

toInt256(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int256, the result over or under flows. This isn't considered an error.

Returns

A 256-bit signed integer. Int256.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt256(-256),
    toInt256(-256.256),
    toInt256('-256')

Result:

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

toInt256OrZero

Converts an input value to a 256-bit signed integer, returning 0 if the conversion fails.

Syntax

toInt256OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int256, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 256-bit signed integer if successful, otherwise 0. Int256.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt256OrZero('-256'),
    toInt256OrZero('abc')

Result:

Row 1:
──────
toInt256OrZero('-256'): -256
toInt256OrZero('abc'):  0

toInt256OrNull

Converts an input value to a 256-bit signed integer, returning NULL if the conversion fails.

Syntax

toInt256OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of Int256, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 256-bit signed integer if successful, otherwise NULL. Int256 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toInt256OrNull('-256'),
    toInt256OrNull('abc')

Result:

Row 1:
──────
toInt256OrNull('-256'): -256
toInt256OrNull('abc'):  ᴺᵁᴸᴸ

toInt256OrDefault

Converts an input value to a 256-bit signed integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toInt256OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The Int256 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of Int256, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 256-bit signed integer if successful, otherwise the default value (if provided) or 0. Int256.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

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

Result:

Row 1:
──────
toInt256OrDefault('-256', CAST('-1', 'Int256')): -256
toInt256OrDefault('abc', CAST('-1', 'Int256')):  -1

toUInt8

Converts an input value to an 8-bit unsigned integer. This function will throw an error if the conversion is not possible.

Syntax

toUInt8(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt8, overflow or underflow of the result occurs. This isn't considered an error. For example: SELECT toUInt8(256) == 0;.

Returns

An 8-bit unsigned integer. UInt8.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt8(8),
    toUInt8(8.8),
    toUInt8('8')

Result:

Row 1:
──────
toUInt8(8):   8
toUInt8(8.8): 8
toUInt8('8'): 8

toUInt8OrZero

Converts an input value to an 8-bit unsigned integer, returning 0 if the conversion fails.

Syntax

toUInt8OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt8, overflow or underflow of the result occurs. This isn't considered an error.

Returns

An 8-bit unsigned integer if successful, otherwise 0. UInt8.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt8OrZero('-8'),
    toUInt8OrZero('abc')

Result:

Row 1:
──────
toUInt8OrZero('-8'):  0
toUInt8OrZero('abc'): 0

toUInt8OrNull

Converts an input value to an 8-bit unsigned integer, returning NULL if the conversion fails.

Syntax

toUInt8OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt8, overflow or underflow of the result occurs. This isn't considered an error.

Returns

An 8-bit unsigned integer if successful, otherwise NULL. UInt8 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt8OrNull('8'),
    toUInt8OrNull('abc')

Result:

Row 1:
──────
toUInt8OrNull('8'):   8
toUInt8OrNull('abc'): ᴺᵁᴸᴸ

toUInt8OrDefault

Converts an input value to an 8-bit unsigned integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toUInt8OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The UInt8 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of UInt8, overflow or underflow of the result occurs. This isn't considered an error.

Returns

An 8-bit unsigned integer if successful, otherwise the default value (if provided) or 0. UInt8.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toUInt8OrDefault('8', CAST('0', 'UInt8')),
    toUInt8OrDefault('abc', CAST('0', 'UInt8'))

Result:

Row 1:
──────
toUInt8OrDefault('8', CAST('0', 'UInt8')):   8
toUInt8OrDefault('abc', CAST('0', 'UInt8')): 0

toUInt16

Converts an input value to a 16-bit unsigned integer. This function will throw an error if the conversion is not possible.

Syntax

toUInt16(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt16, overflow or underflow of the result occurs. This isn't considered an error. For example: SELECT toUInt16(65536) == 0;.

Returns

A 16-bit unsigned integer. UInt16.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt16(16),
    toUInt16(16.16),
    toUInt16('16')

Result:

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

toUInt16OrZero

Converts an input value to a 16-bit unsigned integer, returning 0 if the conversion fails.

Syntax

toUInt16OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt16, overflow or underflow of the result occurs. This isn't considered as an error.

Returns

A 16-bit unsigned integer if successful, otherwise 0. UInt16.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt16OrZero('16'),
    toUInt16OrZero('abc')

Result:

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

toUInt16OrNull

Converts an input value to a 16-bit unsigned integer, returning NULL if the conversion fails.

Syntax

toUInt16OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt16, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 16-bit unsigned integer if successful, otherwise NULL. UInt16 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt16OrNull('16'),
    toUInt16OrNull('abc')

Result:

Row 1:
──────
toUInt16OrNull('16'):  16
toUInt16OrNull('abc'): ᴺᵁᴸᴸ

toUInt16OrDefault

Converts an input value to a 16-bit unsigned integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toUInt16OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The UInt16 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of UInt16, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 16-bit unsigned integer if successful, otherwise the default value (if provided) or 0. UInt16.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

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

Result:

Row 1:
──────
toUInt16OrDefault('16', CAST('0', 'UInt16')):  16
toUInt16OrDefault('abc', CAST('0', 'UInt16')): 0

toUInt32

Converts an input value to a 32-bit unsigned integer. This function will throw an error if the conversion is not possible.

Syntax

toUInt32(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt32, the result over or under flows. This isn't considered an error. For example: SELECT toUInt32(4294967296) == 0;

Returns

A 32-bit unsigned integer. UInt32.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt32(32),
    toUInt32(32.32),
    toUInt32('32')

Result:

Row 1:
──────
toUInt32(32):    32
toUInt32(32.32): 32
toUInt32('32'):  32

toUInt32OrZero

Converts an input value to a 32-bit unsigned integer, returning 0 if the conversion fails.

Syntax

toUInt32OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt32, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 32-bit unsigned integer if successful, otherwise 0. UInt32.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt32OrZero('32'),
    toUInt32OrZero('abc')

Result:

Row 1:
──────
toUInt32OrZero('32'):  32
toUInt32OrZero('abc'): 0

toUInt32OrNull

Converts an input value to a 32-bit unsigned integer, returning NULL if the conversion fails.

Syntax

toUInt32OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt32, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 32-bit unsigned integer if successful, otherwise NULL. UInt32 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt32OrNull('32'),
    toUInt32OrNull('abc')

Result:

Row 1:
──────
toUInt32OrNull('32'):  32
toUInt32OrNull('abc'): ᴺᵁᴸᴸ

toUInt32OrDefault

Converts an input value to a 32-bit unsigned integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toUInt32OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The UInt32 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of UInt32, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 32-bit unsigned integer if successful, otherwise the default value (if provided) or 0. UInt32.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toUInt32OrDefault('32', CAST('0', 'UInt32')),
    toUInt32OrDefault('abc', CAST('0', 'UInt32'))

Result:

Row 1:
──────
toUInt32OrDefault('32', CAST('0', 'UInt32')):  32
toUInt32OrDefault('abc', CAST('0', 'UInt32')): 0

toUInt64

Converts an input value to a 64-bit unsigned integer. This function will throw an error if the conversion is not possible.

Syntax

toUInt64(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt64, the result over or under flows. This isn't considered an error. For example: SELECT toUInt64(18446744073709551616) == 0;

Returns

A 64-bit unsigned integer. UInt64.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt64(64),
    toUInt64(64.64),
    toUInt64('64')

Result:

Row 1:
──────
toUInt64(64):    64
toUInt64(64.64): 64
toUInt64('64'):  64

toUInt64OrZero

Converts an input value to a 64-bit unsigned integer, returning 0 if the conversion fails.

Syntax

toUInt64OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt64, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 64-bit unsigned integer if successful, otherwise 0. UInt64.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt64OrZero('64'),
    toUInt64OrZero('abc')

Result:

Row 1:
──────
toUInt64OrZero('64'):  64
toUInt64OrZero('abc'): 0

toUInt64OrNull

Converts an input value to a 64-bit unsigned integer, returning NULL if the conversion fails.

Syntax

toUInt64OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt64, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 64-bit unsigned integer if successful, otherwise NULL. UInt64 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt64OrNull('64'),
    toUInt64OrNull('abc')

Result:

Row 1:
──────
toUInt64OrNull('64'):  64
toUInt64OrNull('abc'): ᴺᵁᴸᴸ

toUInt64OrDefault

Converts an input value to a 64-bit unsigned integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toUInt64OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The UInt64 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of UInt64, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 64-bit unsigned integer if successful, otherwise the default value (if provided) or 0. UInt64.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toUInt64OrDefault('64', CAST('0', 'UInt64')),
    toUInt64OrDefault('abc', CAST('0', 'UInt64'))

Result:

Row 1:
──────
toUInt64OrDefault('64', CAST('0', 'UInt64')):  64
toUInt64OrDefault('abc', CAST('0', 'UInt64')): 0

toUInt128

Converts an input value to a 128-bit unsigned integer. This function will throw an error if the conversion is not possible.

Syntax

toUInt128(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt128, the result over or under flows. This isn't considered an error.

Returns

A 128-bit unsigned integer. UInt128.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt128(128),
    toUInt128(128.8),
    toUInt128('128')

Result:

Row 1:
──────
toUInt128(128):   128
toUInt128(128.8): 128
toUInt128('128'): 128

toUInt128OrZero

Converts an input value to a 128-bit unsigned integer, returning 0 if the conversion fails.

Syntax

toUInt128OrZero(expr)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt128, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 128-bit unsigned integer if successful, otherwise 0. UInt128.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt128OrZero('128'),
    toUInt128OrZero('abc')

Result:

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

toUInt128OrNull

Converts an input value to a 128-bit unsigned integer, returning NULL if the conversion fails.

Syntax

toUInt128OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt128, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 128-bit unsigned integer if successful, otherwise NULL. UInt128 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt128OrNull('128'),
    toUInt128OrNull('abc')

Result:

Row 1:
──────
toUInt128OrNull('128'): 128
toUInt128OrNull('abc'): ᴺᵁᴸᴸ

toUInt128OrDefault

Converts an input value to a 128-bit unsigned integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toUInt128OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The UInt128 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of UInt128, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 128-bit unsigned integer if successful, otherwise the default value (if provided) or 0. UInt128.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toUInt128OrDefault('128', CAST('0', 'UInt128')),
    toUInt128OrDefault('abc', CAST('0', 'UInt128'))

Result:

Row 1:
──────
toUInt128OrDefault('128', CAST('0', 'UInt128')): 128
toUInt128OrDefault('abc', CAST('0', 'UInt128')): 0

toUInt256

Converts an input value to a 256-bit unsigned integer. This function will throw an error if the conversion is not possible.

Syntax

toUInt256(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations: Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt256, the result over or under flows. This isn't considered an error.

Returns

A 256-bit unsigned integer. UInt256.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt256(256),
    toUInt256(256.256),
    toUInt256('256')

Result:

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

toUInt256OrZero

Converts an input value to a 256-bit unsigned integer, returning 0 if the conversion fails.

Syntax

toUInt256OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt256, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 256-bit unsigned integer if successful, otherwise 0. UInt256.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt256OrZero('256'),
    toUInt256OrZero('abc')

Result:

Row 1:
──────
toUInt256OrZero('256'): 256
toUInt256OrZero('abc'): 0

toUInt256OrNull

Converts an input value to a 256-bit unsigned integer, returning NULL if the conversion fails.

Syntax

toUInt256OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers.
    • Unsupported string representations (return NULL): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.

If the input value can't be represented within the bounds of UInt256, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 256-bit unsigned integer if successful, otherwise NULL. UInt256 / Nullable.

The function uses rounding towards zero, meaning it truncates fractional digits of numbers.

Example

SELECT
    toUInt256OrNull('256'),
    toUInt256OrNull('abc')

Result:

Row 1:
──────
toUInt256OrNull('256'): 256
toUInt256OrNull('abc'): ᴺᵁᴸᴸ

toUInt256OrDefault

Converts an input value to a 256-bit unsigned integer, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toUInt256OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers.
    • Unsupported string representations (return default or 0): Floating-point numbers (e.g., '1.23'), NaN, Inf, binary, or hexadecimal values.
  • default (optional): The UInt256 value to return if the conversion of expr is unsuccessful.

If the input value can't be represented within the bounds of UInt256, overflow or underflow of the result occurs. This isn't considered an error.

Returns

A 256-bit unsigned integer if successful, otherwise the default value (if provided) or 0. UInt256.

  • The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
  • The default value type should be the same as the cast type.

Example

SELECT
    toUInt256OrDefault('-256', CAST('0', 'UInt256')),
    toUInt256OrDefault('abc', CAST('0', 'UInt256'))

Result:

Row 1:
──────
toUInt256OrDefault('-256', CAST('0', 'UInt256')): 0
toUInt256OrDefault('abc', CAST('0', 'UInt256')):  0

toFloat32

Converts an input value to a 32-bit floating-point number. This function will throw an error if the conversion is not possible.

Syntax

toFloat32(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf (case-insensitive).
    • Unsupported string representations: Binary or hexadecimal values.

Returns

A 32-bit floating-point number. Float32.

Example

SELECT
    toFloat32(42.7),
    toFloat32('42.7'),
    toFloat32('NaN')

Result:

Row 1:
──────
toFloat32(42.7):   42.7
toFloat32('42.7'): 42.7
toFloat32('NaN'):  nan

toFloat32OrZero

Converts an input value to a 32-bit floating-point number, returning 0 if the conversion fails.

Syntax

toFloat32OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf.
    • Unsupported string representations (return 0): Binary or hexadecimal values.

Returns

A 32-bit floating-point number if successful, otherwise 0. Float32.

Example

SELECT
    toFloat32OrZero('42.7'),
    toFloat32OrZero('abc')

Result:

Row 1:
──────
toFloat32OrZero('42.7'): 42.7
toFloat32OrZero('abc'):  0

toFloat32OrNull

Converts an input value to a 32-bit floating-point number, returning NULL if the conversion fails.

Syntax

toFloat32OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf.
    • Unsupported string representations (return NULL): Binary or hexadecimal values.

Returns

A 32-bit floating-point number if successful, otherwise NULL. Float32 / Nullable.

Example

SELECT
    toFloat32OrNull('42.7'),
    toFloat32OrNull('abc')

Result:

Row 1:
──────
toFloat32OrNull('42.7'): 42.7
toFloat32OrNull('abc'):  ᴺᵁᴸᴸ

toFloat32OrDefault

Converts an input value to a 32-bit floating-point number, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toFloat32OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf (case-insensitive).
    • Unsupported string representations (return default or 0): Binary or hexadecimal values.
  • default (optional): The Float32 value to return if the conversion of expr is unsuccessful.

Returns

A 32-bit floating-point number if successful, otherwise the default value (if provided) or 0. Float32.

Example

SELECT
    toFloat32OrDefault('8', CAST('0', 'Float32')),
    toFloat32OrDefault('abc', CAST('0', 'Float32'))

Result:

Row 1:
──────
toFloat32OrDefault('8', CAST('0', 'Float32')):   8
toFloat32OrDefault('abc', CAST('0', 'Float32')): 0

toFloat64

Converts an input value to a 64-bit floating-point number. This function will throw an error if the conversion is not possible.

Syntax

toFloat64(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf (case-insensitive).
    • Unsupported string representations: Binary or hexadecimal values.

Returns

A 64-bit floating-point number. Float64.

Example

SELECT
    toFloat64(42.7),
    toFloat64('42.7'),
    toFloat64('NaN')

Result:

Row 1:
──────
toFloat64(42.7):   42.7
toFloat64('42.7'): 42.7
toFloat64('NaN'):  nan

toFloat64OrZero

Converts an input value to a 64-bit floating-point number, returning 0 if the conversion fails.

Syntax

toFloat64OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf.
    • Unsupported string representations (return 0): Binary or hexadecimal values.

Returns

A 64-bit floating-point number if successful, otherwise 0. Float64.

Example

SELECT
    toFloat64OrZero('42.7'),
    toFloat64OrZero('abc')

Result:

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

toFloat64OrNull

Converts an input value to a 64-bit floating-point number, returning NULL if the conversion fails.

Syntax

toFloat64OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf.
    • Unsupported string representations (return NULL): Binary or hexadecimal values.

Returns

A 64-bit floating-point number if successful, otherwise NULL. Float64 / Nullable.

Example

SELECT
    toFloat64OrNull('42.7'),
    toFloat64OrNull('abc')

Result:

Row 1:
──────
toFloat64OrNull('42.7'): 42.7
toFloat64OrNull('abc'):  ᴺᵁᴸᴸ

toFloat64OrDefault

Converts an input value to a 64-bit floating-point number, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toFloat64OrDefault(expr[, default])

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf (case-insensitive).
    • Unsupported string representations (return default or 0): Binary or hexadecimal values.
  • default (optional): The Float64 value to return if the conversion of expr is unsuccessful.

Returns

A 64-bit floating-point number if successful, otherwise the default value (if provided) or 0. Float64.

Example

SELECT
    toFloat64OrDefault('8', CAST('0', 'Float64')),
    toFloat64OrDefault('abc', CAST('0', 'Float64'))

Result:

Row 1:
──────
toFloat64OrDefault('8', CAST('0', 'Float64')):   8
toFloat64OrDefault('abc', CAST('0', 'Float64')): 0

toBFloat16

Converts an input value to a 16-bit brain floating-point number. This function will throw an error if the conversion is not possible.

Syntax

toBFloat16(expr)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers, NaN, Inf (case-insensitive).

Returns

A 16-bit brain floating-point value. BFloat16.

Example

SELECT toBFloat16(toFloat32(42.7))

Result:

┌─toBFloat16(toFloat32(42.7))─┐
│                       42.5 │
└────────────────────────────┘

toBFloat16OrZero

Converts a string input value to a 16-bit brain floating-point number, returning 0 if the conversion fails.

Syntax

toBFloat16OrZero(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Numeric values (integers, floating-point numbers, NaN, Inf).
    • Unsupported string representations (return 0): Binary or hexadecimal values.

Returns

A 16-bit brain floating-point value if successful, otherwise 0. BFloat16.

Example

SELECT toBFloat16OrZero('0x5E'), toBFloat16OrZero('12.3'), toBFloat16OrZero('12.3456789')

Result:

┌─toBFloat16OrZero('0x5E')─┬─toBFloat16OrZero('12.3')─┬─toBFloat16OrZero('12.3456789')─┐
│                        0 │                    12.25 │                         12.3125 │
└──────────────────────────┴──────────────────────────┴────────────────────────────────┘

toBFloat16OrNull

Converts a string input value to a 16-bit brain floating-point number, returning NULL if the conversion fails.

Syntax

toBFloat16OrNull(x)

Arguments

  • x: A string representation of a number.
    • Supported string representations: Numeric values (integers, floating-point numbers, NaN, Inf).
    • Unsupported string representations (return NULL): Binary or hexadecimal values.

Returns

A 16-bit brain floating-point value if successful, otherwise NULL. BFloat16 / Nullable.

Example

SELECT toBFloat16OrNull('0x5E'), toBFloat16OrNull('12.3'), toBFloat16OrNull('12.3456789')

Result:

┌─toBFloat16OrNull('0x5E')─┬─toBFloat16OrNull('12.3')─┬─toBFloat16OrNull('12.3456789')─┐
│                     ᴺᵁᴸᴸ │                    12.25 │                         12.3125 │
└──────────────────────────┴──────────────────────────┴────────────────────────────────┘

toDate

Converts an input value to a Date data type.

Syntax

toDate(expr[, time_zone])

Arguments

  • expr: The value to convert. Can be a String, Int, Date, or DateTime / DateTime64.
    • If expr is a DateTime or DateTime64, it extracts only the date component.
    • If expr is a String, it parses the string as a date or datetime and extracts the date component.
    • If expr is a number greater than or equal to 65536, it's interpreted as a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC) and converted to a date.
    • If expr is a number less than 65536, it's interpreted as the number of days since 1970-01-01.
  • time_zone (optional): A string specifying the timezone for interpreting Unix timestamps or datetime strings. String.

Returns

A date value. Date.

Example

SELECT
    toDate('2022-12-30 01:02:03') AS date_from_datetime_string,
    toDate(1672404714, 'Pacific/Apia') AS date_from_unix_timestamp_with_timezone,
    toDate(12345) AS date_from_days_since_epoch

Result:

┌─date_from_datetime_string─┬─date_from_unix_timestamp_with_timezone─┬─date_from_days_since_epoch─┐
│                2022-12-30 │                             2022-12-31 │                 2003-10-20 │
└───────────────────────────┴────────────────────────────────────────┴────────────────────────────┘

toDateOrZero

Converts a string to a Date data type, returning the minimum Date value (1970-01-01) if the conversion fails.

Syntax

toDateOrZero(str)

Arguments

  • str: The string to convert to a date. String.

Returns

A date value if successful, otherwise 1970-01-01. Date.

Example

SELECT toDateOrZero('2022-12-30'), toDateOrZero('invalid_date')

Result:

┌─toDateOrZero('2022-12-30')─┬─toDateOrZero('invalid_date')─┐
│                 2022-12-30 │                   1970-01-01 │
└────────────────────────────┴──────────────────────────────┘

toDateOrNull

Converts a string to a Date data type, returning NULL if the conversion fails.

Syntax

toDateOrNull(str)

Arguments

  • str: The string to convert to a date. String.

Returns

A date value if successful, otherwise NULL. Date / Nullable.

Example

SELECT toDateOrNull('2022-12-30'), toDateOrNull('invalid_date')

Result:

┌─toDateOrNull('2022-12-30')─┬─toDateOrNull('invalid_date')─┐
│                 2022-12-30 │                         ᴺᵁᴸᴸ │
└────────────────────────────┴──────────────────────────────┘

toDateOrDefault

Converts an input value to a Date data type, returning a specified default value if the conversion fails. If no default is provided, it returns the minimum Date value (1970-01-01).

Syntax

toDateOrDefault(expr[, default_value])

Arguments

  • expr: The value to convert to a date. Can be a String, Int, Date, or DateTime / DateTime64.
  • default_value (optional): The Date value to return if the conversion of expr is unsuccessful.

Returns

A date value if successful, otherwise the default_value (if provided) or 1970-01-01. Date.

Example

SELECT
    toDateOrDefault('2022-12-30'),
    toDateOrDefault('invalid_date', CAST('2023-01-01', 'Date'))

Result:

┌─toDateOrDefault('2022-12-30')─┬─toDateOrDefault('invalid_date', CAST('2023-01-01', 'Date'))─┐
│                    2022-12-30 │                                                  2023-01-01 │
└───────────────────────────────┴───────────────────────────────────────────────────────────┘

toDateTime

Converts an input value to a DateTime data type.

Syntax

toDateTime(expr[, time_zone])

Arguments

  • expr: The value to convert. Can be a String, Int, Date, or DateTime.
    • If expr is a number, it's interpreted as a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).
    • If expr is a string, it's parsed as a datetime string. Parsing of short numeric strings (up to 4 digits) is disabled to avoid ambiguity with Unix timestamps.
  • time_zone (optional): A string specifying the timezone for interpreting Unix timestamps or datetime strings. String.

Returns

A datetime value. DateTime.

Example

SELECT
    toDateTime('2022-12-30 13:44:17'),
    toDateTime(1685457500, 'UTC')

Result:

┌─toDateTime('2022-12-30 13:44:17')─┬─toDateTime(1685457500, 'UTC')─┐
│               2022-12-30 13:44:17 │           2023-05-30 14:38:20 │
└───────────────────────────────────┴───────────────────────────────┘

toDateTimeOrZero

Converts a string to a DateTime data type, returning the minimum DateTime value (1970-01-01 00:00:00) if the conversion fails.

Syntax

toDateTimeOrZero(str)

Arguments

  • str: The string to convert to a datetime. String.

Returns

A datetime value if successful, otherwise 1970-01-01 00:00:00. DateTime.

Example

SELECT toDateTimeOrZero('2022-12-30 13:44:17'), toDateTimeOrZero('invalid_datetime')

Result:

┌─toDateTimeOrZero('2022-12-30 13:44:17')─┬─toDateTimeOrZero('invalid_datetime')─┐
│                     2022-12-30 13:44:17 │                    1970-01-01 00:00:00 │
└─────────────────────────────────────────┴───────────────────────────────────────┘

toDateTimeOrNull

Converts a string to a DateTime data type, returning NULL if the conversion fails.

Syntax

toDateTimeOrNull(str)

Arguments

  • str: The string to convert to a datetime. String.

Returns

A datetime value if successful, otherwise NULL. DateTime / Nullable.

Example

SELECT toDateTimeOrNull('2022-12-30 13:44:17'), toDateTimeOrNull('invalid_datetime')

Result:

┌─toDateTimeOrNull('2022-12-30 13:44:17')─┬─toDateTimeOrNull('invalid_datetime')─┐
│                     2022-12-30 13:44:17 │                                 ᴺᵁᴸᴸ │
└─────────────────────────────────────────┴───────────────────────────────────────┘

toDateTimeOrDefault

Converts an input value to a DateTime data type, returning a specified default value if the conversion fails. If no default is provided, it returns the minimum DateTime value (1970-01-01 00:00:00).

Syntax

toDateTimeOrDefault(expr[, time_zone [, default_value]])

Arguments

  • expr: The value to convert. Can be a String, Int, Date, or DateTime.
  • time_zone (optional): A string specifying the timezone for interpreting Unix timestamps or datetime strings. String.
  • default_value (optional): The DateTime value to return if the conversion of expr is unsuccessful.

Returns

A datetime value if successful, otherwise the default_value (if provided) or 1970-01-01 00:00:00. DateTime.

Example

SELECT
    toDateTimeOrDefault('2022-12-30 13:44:17'),
    toDateTimeOrDefault('invalid_datetime', 'UTC', CAST('2023-01-01', 'DateTime(\'UTC\')'))

Result:

┌─toDateTimeOrDefault('2022-12-30 13:44:17')─┬─toDateTimeOrDefault('invalid_datetime', 'UTC', CAST('2023-01-01', 'DateTime(\'UTC\')'))─┐
│                        2022-12-30 13:44:17 │                                                     2023-01-01 00:00:00 │
└────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────┘

toDate32

Converts an input value to a Date32 data type. This function will throw an error if the conversion is not possible.

Syntax

toDate32(expr)

Arguments

  • expr: The value to convert. Can be a String, UInt32, or Date.
    • If expr is a Date, its range is considered.
    • If expr is a String, it parses the string as a date.

Returns

A calendar date. Date32.

Example

SELECT
    toDate32('1955-01-01') AS valid_date,
    toDate32('1899-01-01') AS out_of_range_date,
    toDate32(toDate('1899-01-01')) AS date_from_date_type_out_of_range

Result:

┌─valid_date─┬─out_of_range_date─┬─date_from_date_type_out_of_range─┐
│ 1955-01-01 │        1900-01-01 │                         1970-01-01 │
└────────────┴───────────────────┴──────────────────────────────────┘

toDate32OrZero

Converts an input value to a Date32 data type, returning the minimum Date32 value (1900-01-01) if the conversion fails.

Syntax

toDate32OrZero(expr)

Arguments

Returns

A calendar date if successful, otherwise 1900-01-01. Date32.

Example

SELECT toDate32OrZero('1899-01-01'), toDate32OrZero('invalid_date')

Result:

┌─toDate32OrZero('1899-01-01')─┬─toDate32OrZero('invalid_date')─┐
│                   1900-01-01 │                     1900-01-01 │
└──────────────────────────────┴────────────────────────────────┘

toDate32OrNull

Converts an input value to a Date32 data type, returning NULL if the conversion fails.

Syntax

toDate32OrNull(expr)

Arguments

Returns

A calendar date if successful, otherwise NULL. Date32 / Nullable.

Example

SELECT toDate32OrNull('1955-01-01'), toDate32OrNull('invalid_date')

Result:

┌─toDate32OrNull('1955-01-01')─┬─toDate32OrNull('invalid_date')─┐
│                   1955-01-01 │                           ᴺᵁᴸᴸ │
└──────────────────────────────┴────────────────────────────────┘

toDate32OrDefault

Converts an input value to a Date32 data type, returning a specified default value if the conversion fails. If no default is provided, it returns the minimum Date32 value (1900-01-01).

Syntax

toDate32OrDefault(expr[, default_value])

Arguments

  • expr: The value to convert. Can be a String, UInt32, or Date.
  • default_value (optional): The Date32 value to return if the conversion of expr is unsuccessful.

Returns

A calendar date if successful, otherwise the default_value (if provided) or 1900-01-01. Date32.

Example

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

Result:

┌─toDate32OrDefault('1930-01-01', toDate32('2020-01-01'))─┬─toDate32OrDefault('xx1930-01-01', toDate32('2020-01-01'))─┐
│                                              1930-01-01 │                                                2020-01-01 │
└─────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────┘

toDateTime64

Converts an input value to a DateTime64 data type with specified precision. This function will throw an error if the conversion is not possible.

Syntax

toDateTime64(expr, scale, [timezone])

Arguments

  • expr: The value to convert. Can be a String, UInt32, Float, or DateTime.
  • scale: An integer from 0 to 9 representing the number of decimal places for sub-second precision. UInt8.
  • timezone (optional): A string specifying the timezone for interpreting the datetime. String.

Returns

A calendar date and time with sub-second precision. DateTime64.

Example

SELECT
    toDateTime64('1955-01-01 00:00:00.000', 3) AS datetime_from_string,
    toDateTime64(1546300800.000, 3) AS datetime_from_float_unix_timestamp,
    toDateTime64('2019-01-01 00:00:00', 3, 'Asia/Istanbul') AS datetime_with_timezone

Result:

┌─datetime_from_string─┬─datetime_from_float_unix_timestamp─┬─datetime_with_timezone─┐
│ 1955-01-01 00:00:00.000 │          2019-01-01 00:00:00.000 │ 2019-01-01 00:00:00.000 │
└───────────────────────┴──────────────────────────────────┴────────────────────────┘

toDateTime64OrZero

Converts an input value to a DateTime64 data type, returning the minimum DateTime64 value (1970-01-01 01:00:00.000) if the conversion fails.

Syntax

toDateTime64OrZero(expr, scale, [timezone])

Arguments

  • expr: The value to convert. Can be a String, UInt32, Float, or DateTime.
  • scale: An integer from 0 to 9 representing the number of decimal places for sub-second precision. UInt8.
  • timezone (optional): A string specifying the timezone for interpreting the datetime. String.

Returns

A calendar date and time with sub-second precision if successful, otherwise 1970-01-01 01:00:00.000. DateTime64.

Example

SELECT toDateTime64OrZero('2008-10-12 00:00:00 00:30:30', 3) AS invalid_arg

Result:

┌─────────────invalid_arg─┐
│ 1970-01-01 01:00:00.000 │
└─────────────────────────┘

toDateTime64OrNull

Converts an input value to a DateTime64 data type, returning NULL if the conversion fails.

Syntax

toDateTime64OrNull(expr, scale, [timezone])

Arguments

  • expr: The value to convert. Can be a String, UInt32, Float, or DateTime.
  • scale: An integer from 0 to 9 representing the number of decimal places for sub-second precision. UInt8.
  • timezone (optional): A string specifying the timezone for interpreting the datetime. String.

Returns

A calendar date and time with sub-second precision if successful, otherwise NULL. DateTime64 / Nullable.

Example

SELECT
    toDateTime64OrNull('1976-10-18 00:00:00.30', 3) AS valid_arg,
    toDateTime64OrNull('1976-10-18 00:00:00 30', 3) AS invalid_arg

Result:

┌───────────────valid_arg─┬─invalid_arg─┐
│ 1976-10-18 00:00:00.300 │        ᴺᵁᴸᴸ │
└─────────────────────────┴─────────────┘

toDateTime64OrDefault

Converts an input value to a DateTime64 data type, returning a specified default value if the conversion fails. If no default is provided, it returns the minimum DateTime64 value (1970-01-01 01:00:00.000).

Syntax

toDateTime64OrDefault(expr, scale, [timezone, default_value])

Arguments

  • expr: The value to convert. Can be a String, UInt32, Float, or DateTime.
  • scale: An integer from 0 to 9 representing the number of decimal places for sub-second precision. UInt8.
  • timezone (optional): A string specifying the timezone for interpreting the datetime. String.
  • default_value (optional): The DateTime64 value to return if the conversion of expr is unsuccessful.

Returns

A calendar date and time with sub-second precision if successful, otherwise the default_value (if provided) or 1970-01-01 01:00:00.000. DateTime64.

Example

SELECT
    toDateTime64OrDefault('1976-10-18 00:00:00 30', 3) AS invalid_arg,
    toDateTime64OrDefault('1976-10-18 00:00:00 30', 3, 'UTC', toDateTime64('2001-01-01 00:00:00.00',3)) AS invalid_arg_with_default

Result:

┌─────────────invalid_arg─┬─invalid_arg_with_default─┐
│ 1970-01-01 01:00:00.000 │  2000-12-31 23:00:00.000 │
└─────────────────────────┴──────────────────────────┘

toDecimal32

Converts an input value to a Decimal32 data type with a specified scale. This function will throw an error if the conversion is not possible or if the integer part overflows.

Syntax

toDecimal32(expr, S)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations: NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 9 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal32: ( -1 * 10^(9 - S), 1 * 10^(9 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an exception.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal32(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal32('1.15', 2) = 1.15

Returns

A Decimal32 value with the specified scale. Decimal32(S).

Example

SELECT
    toDecimal32(2, 1) AS a,
    toDecimal32(4.2, 2) AS b,
    toDecimal32('4.2', 3) AS c

Result:

┌─a─┬─b───┬─c───┐
│ 2 │ 4.2 │ 4.2 │
└───┴─────┴─────┘

toDecimal32OrZero

Converts an input value to a Decimal32 data type with a specified scale, returning 0 if the conversion fails.

Syntax

toDecimal32OrZero(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 9 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal32: ( -1 * 10^(9 - S), 1 * 10^(9 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal32 value if successful, otherwise 0 with S decimal places. Decimal32(S).

Example

SELECT
    toDecimal32OrZero(toString(-1.111), 5) AS a,
    toDecimal32OrZero(toString('Inf'), 5) as b

Result:

┌───────a─┬─b─┐
│ -1.11100 │ 0 │
└─────────┴───┘

toDecimal32OrNull

Converts an input value to a Decimal32 data type with a specified scale, returning NULL if the conversion fails.

Syntax

toDecimal32OrNull(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return NULL): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 9 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal32: ( -1 * 10^(9 - S), 1 * 10^(9 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal32 value if successful, otherwise NULL. Nullable(Decimal32(S)).

Example

SELECT
    toDecimal32OrNull(toString(-1.111), 5) AS a,
    toDecimal32OrNull(toString('Inf'), 5) as b

Result:

┌───────a─┬─b─┐
│ -1.11100 │ ᴺᵁᴸᴸ │
└─────────┴───┘

toDecimal32OrDefault

Converts an input value to a Decimal32 data type with a specified scale, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toDecimal32OrDefault(expr, S[, default])

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return default or 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 9 representing the number of decimal places for the fractional part. UInt8.
  • default (optional): The Decimal32(S) value to return if the conversion of expr is unsuccessful.

An overflow can occur if the value of expr exceeds the bounds of Decimal32: ( -1 * 10^(9 - S), 1 * 10^(9 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal32OrDefault(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal32OrDefault('1.15', 2) = 1.15

Returns

A Decimal32 value if successful, otherwise the default value (if provided) or 0. Decimal32(S).

Example

SELECT
    toDecimal32OrDefault(toString(0.0001), 5) AS a,
    toDecimal32OrDefault('Inf', 0, CAST('-1', 'Decimal32(0)')) AS b

Result:

┌────────a─┬─b─┐
│ 0.00010 │ -1 │
└─────────┴───┘

toDecimal64

Converts an input value to a Decimal64 data type with a specified scale. This function will throw an error if the conversion is not possible or if the integer part overflows.

Syntax

toDecimal64(expr, S)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations: NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 18 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal64: ( -1 * 10^(18 - S), 1 * 10^(18 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an exception.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal64(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal64('1.15', 2) = 1.15

Returns

A Decimal64 value with the specified scale. Decimal64(S).

Example

SELECT
    toDecimal64(2, 1) AS a,
    toDecimal64(4.2, 2) AS b,
    toDecimal64('4.2', 3) AS c

Result:

┌─a─┬─b───┬─c───┐
│ 2 │ 4.2 │ 4.2 │
└───┴─────┴─────┘

toDecimal64OrZero

Converts an input value to a Decimal64 data type with a specified scale, returning 0 if the conversion fails.

Syntax

toDecimal64OrZero(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 18 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal64: ( -1 * 10^(18 - S), 1 * 10^(18 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal64 value if successful, otherwise 0 with S decimal places. Decimal64(S).

Example

SELECT
    toDecimal64OrZero(toString(0.0001), 18) AS a,
    toDecimal64OrZero(toString('Inf'), 18) as b

Result:

┌──────────a─┬─b─┐
│ 0.000100000000000000 │ 0 │
└────────────┴───┘

toDecimal64OrNull

Converts an input value to a Decimal64 data type with a specified scale, returning NULL if the conversion fails.

Syntax

toDecimal64OrNull(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return NULL): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 18 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal64: ( -1 * 10^(18 - S), 1 * 10^(18 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal64 value if successful, otherwise NULL. Nullable(Decimal64(S)).

Example

SELECT
    toDecimal64OrNull(toString(0.0001), 18) AS a,
    toDecimal64OrNull(toString('Inf'), 18) as b

Result:

┌──────────a─┬─b─┐
│ 0.000100000000000000 │ ᴺᵁᴸᴸ │
└────────────┴───┘

toDecimal64OrDefault

Converts an input value to a Decimal64 data type with a specified scale, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toDecimal64OrDefault(expr, S[, default])

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return default or 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 18 representing the number of decimal places for the fractional part. UInt8.
  • default (optional): The Decimal64(S) value to return if the conversion of expr is unsuccessful.

An overflow can occur if the value of expr exceeds the bounds of Decimal64: ( -1 * 10^(18 - S), 1 * 10^(18 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal64OrDefault(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal64OrDefault('1.15', 2) = 1.15

Returns

A Decimal64 value if successful, otherwise the default value (if provided) or 0. Decimal64(S).

Example

SELECT
    toDecimal64OrDefault(toString(0.0001), 18) AS a,
    toDecimal64OrDefault('Inf', 0, CAST('-1', 'Decimal64(0)')) AS b

Result:

┌──────────a─┬─b─┐
│ 0.000100000000000000 │ -1 │
└────────────┴───┘

toDecimal128

Converts an input value to a Decimal128 data type with a specified scale. This function will throw an error if the conversion is not possible or if the integer part overflows.

Syntax

toDecimal128(expr, S)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations: NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 38 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal128: ( -1 * 10^(38 - S), 1 * 10^(38 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an exception.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal128(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal128('1.15', 2) = 1.15

Returns

A Decimal128 value with the specified scale. Decimal128(S).

Example

SELECT
    toDecimal128(99, 1) AS a,
    toDecimal128(99.67, 2) AS b,
    toDecimal128('99.67', 3) AS c

Result:

┌─a─┬─b─────┬─c─────┐
│ 99 │ 99.67 │ 99.67 │
└───┴───────┴───────┘

toDecimal128OrZero

Converts an input value to a Decimal128 data type with a specified scale, returning 0 if the conversion fails.

Syntax

toDecimal128OrZero(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 38 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal128: ( -1 * 10^(38 - S), 1 * 10^(38 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal128 value if successful, otherwise 0 with S decimal places. Decimal128(S).

Example

SELECT
    toDecimal128OrZero(toString(0.0001), 38) AS a,
    toDecimal128OrZero(toString('Inf'), 38) as b

Result:

┌──────────a─┬─b─┐
│ 0.0001000000000000000000000000000000000000 │ 0 │
└────────────┴───┘

toDecimal128OrNull

Converts an input value to a Decimal128 data type with a specified scale, returning NULL if the conversion fails.

Syntax

toDecimal128OrNull(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return NULL): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 38 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal128: ( -1 * 10^(38 - S), 1 * 10^(38 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal128 value if successful, otherwise NULL. Nullable(Decimal128(S)).

Example

SELECT
    toDecimal128OrNull(toString(1/42), 38) AS a,
    toDecimal128OrNull(toString('Inf'), 38) as b

Result:

┌──────────a─┬─b─┐
│ 0.02380952380952380800000000000000000000 │ ᴺᵁᴸᴸ │
└────────────┴───┘

toDecimal128OrDefault

Converts an input value to a Decimal128 data type with a specified scale, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toDecimal128OrDefault(expr, S[, default])

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return default or 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 38 representing the number of decimal places for the fractional part. UInt8.
  • default (optional): The Decimal128(S) value to return if the conversion of expr is unsuccessful.

An overflow can occur if the value of expr exceeds the bounds of Decimal128: ( -1 * 10^(38 - S), 1 * 10^(38 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal128OrDefault(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal128OrDefault('1.15', 2) = 1.15

Returns

A Decimal128 value if successful, otherwise the default value (if provided) or 0. Decimal128(S).

Example

SELECT
    toDecimal128OrDefault(toString(1/42), 18) AS a,
    toDecimal128OrDefault('Inf', 0, CAST('-1', 'Decimal128(0)')) AS b

Result:

┌──────────a─┬─b─┐
│ 0.023809523809523808 │ -1 │
└────────────┴───┘

toDecimal256

Converts an input value to a Decimal256 data type with a specified scale. This function will throw an error if the conversion is not possible or if the integer part overflows.

Syntax

toDecimal256(expr, S)

Arguments

  • expr: An expression that evaluates to a number or a string representation of a number.
    • Supported numeric types: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Int256, UInt256, Float32, Float64.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations: NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 76 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal256: ( -1 * 10^(76 - S), 1 * 10^(76 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an exception.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal256(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal256('1.15', 2) = 1.15

Returns

A Decimal256 value with the specified scale. Decimal256(S).

Example

SELECT
    toDecimal256(99, 1) AS a,
    toDecimal256(99.67, 2) AS b,
    toDecimal256('99.67', 3) AS c

Result:

┌─a─┬─b─────┬─c─────┐
│ 99 │ 99.67 │ 99.67 │
└───┴───────┴───────┘

toDecimal256OrZero

Converts an input value to a Decimal256 data type with a specified scale, returning 0 if the conversion fails.

Syntax

toDecimal256OrZero(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 76 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal256: ( -1 * 10^(76 - S), 1 * 10^(76 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal256 value if successful, otherwise 0 with S decimal places. Decimal256(S).

Example

SELECT
    toDecimal256OrZero(toString(0.0001), 76) AS a,
    toDecimal256OrZero(toString('Inf'), 76) as b

Result:

┌──────────a─┬─b─┐
│ 0.0001000000000000000000000000000000000000000000000000000000000000000000000000 │ 0 │
└────────────┴───┘

toDecimal256OrNull

Converts an input value to a Decimal256 data type with a specified scale, returning NULL if the conversion fails.

Syntax

toDecimal256OrNull(expr, S)

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return NULL): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 76 representing the number of decimal places for the fractional part. UInt8.

An overflow can occur if the value of expr exceeds the bounds of Decimal256: ( -1 * 10^(76 - S), 1 * 10^(76 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Returns

A Decimal256 value if successful, otherwise NULL. Nullable(Decimal256(S)).

Example

SELECT
    toDecimal256OrNull(toString(1/42), 76) AS a,
    toDecimal256OrNull(toString('Inf'), 76) as b

Result:

┌──────────a─┬─b─┐
│ 0.02380952380952380800000000000000000000000000000000000000000000000000000000 │ ᴺᵁᴸᴸ │
└────────────┴───┘

toDecimal256OrDefault

Converts an input value to a Decimal256 data type with a specified scale, returning a specified default value if the conversion fails. If no default is provided, it returns 0.

Syntax

toDecimal256OrDefault(expr, S[, default])

Arguments

  • expr: A string representation of a number.
    • Supported string representations: Integers, floating-point numbers.
    • Unsupported string representations (return default or 0): NaN, Inf, binary, or hexadecimal values.
  • S: An integer from 0 to 76 representing the number of decimal places for the fractional part. UInt8.
  • default (optional): The Decimal256(S) value to return if the conversion of expr is unsuccessful.

An overflow can occur if the value of expr exceeds the bounds of Decimal256: ( -1 * 10^(76 - S), 1 * 10^(76 - S) ). Excessive digits in a fraction are discarded (not rounded). Excessive digits in the integer part will lead to an error.

Conversions drop extra digits and could operate in an unexpected way when working with Float32/Float64 inputs as the operations are performed using floating point instructions. For example: toDecimal256OrDefault(1.15, 2) is equal to 1.14 because 1.15 * 100 in floating point is 114.99. You can use a String input so the operations use the underlying integer type: toDecimal256OrDefault('1.15', 2) = 1.15

Returns

A Decimal256 value if successful, otherwise the default value (if provided) or 0. Decimal256(S).

Example

SELECT
    toDecimal256OrDefault(toString(1/42), 76) AS a,
    toDecimal256OrDefault('Inf', 0, CAST('-1', 'Decimal256(0)')) AS b

Result:

┌──────────a─┬─b─┐
│ 0.02380952380952380800000000000000000000000000000000000000000000000000000000 │ -1 │
└────────────┴───┘

toString

Converts a value of any supported data type to its string representation.

Syntax

toString(expr[, time_zone])

Arguments

  • expr: The value to convert. Can be a number, string, date, or datetime.
  • time_zone (optional): For DateTime arguments, a string specifying the timezone for formatting the output. String.

Returns

A string representation of the input value. String.

Example

SELECT
    toString(123) AS num_to_string,
    toString(toDate('2023-01-15')) AS date_to_string,
    toString(toDateTime('2023-01-15 10:30:00'), 'Europe/Berlin') AS datetime_to_string_with_tz

Result:

┌─num_to_string─┬─date_to_string─┬─datetime_to_string_with_tz─┐
│ 123           │ 2023-01-15     │ 2023-01-15 11:30:00        │
└───────────────┴────────────────┴────────────────────────────┘

toFixedString

Converts a string to a FixedString(N) data type, padding with null bytes or throwing an error if the length is mismatched.

Syntax

toFixedString(s, N)

Arguments

  • s: The string to convert. String.
  • N: The desired fixed length of the string. UInt8.

Returns

A fixed-length string. FixedString.

Example

SELECT toFixedString('foo', 8) AS s

Result:

┌─s─────────────┐
│ foo\0\0\0\0\0 │
└───────────────┘

toStringCutToZero

Truncates a string at the first null byte encountered.

Syntax

toStringCutToZero(s)

Arguments

Returns

A string truncated at the first null byte. String.

Example

SELECT toFixedString('foo\0bar', 8) AS s, toStringCutToZero(s) AS s_cut

Result:

┌─s──────────┬─s_cut─┐
│ foo\0bar\0 │ foo   │
└────────────┴───────┘

toDecimalString

Converts a numeric value to its string representation with a specified number of fractional digits.

Syntax

toDecimalString(number, scale)

Arguments

  • number: The numeric value to convert. Can be an Int, UInt, Float, or Decimal.
  • scale: The number of fractional digits to include in the output string. UInt8.
    • Maximum scale for Decimal and integer types is 77.
    • Maximum scale for Float types is 60.

Returns

A string representation of the number with the specified fractional digits. String.

Example

SELECT toDecimalString(CAST('64.32', 'Float64'), 5)

Result:

┌─toDecimalString(CAST('64.32', 'Float64'), 5)─┐
│ 64.32000                                     │
└──────────────────────────────────────────────┘

reinterpretAsUInt8

Performs a byte-level reinterpretation of an input value as an 8-bit unsigned integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsUInt8(x)

Arguments

Returns

The byte-reinterpreted value as an 8-bit unsigned integer. UInt8.

Example

SELECT
    toInt8(257) AS x,
    reinterpretAsUInt8(x) AS res

Result:

┌─x─┬─res─┐
│ 1 │   1 │
└───┴─────┘

reinterpretAsUInt16

Performs a byte-level reinterpretation of an input value as a 16-bit unsigned integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsUInt16(x)

Arguments

Returns

The byte-reinterpreted value as a 16-bit unsigned integer. UInt16.

Example

SELECT
    toUInt8(257) AS x,
    reinterpretAsUInt16(x) AS res

Result:

┌─x─┬─res─┐
│ 1 │   1 │
└───┴─────┘

reinterpretAsUInt32

Performs a byte-level reinterpretation of an input value as a 32-bit unsigned integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsUInt32(x)

Arguments

Returns

The byte-reinterpreted value as a 32-bit unsigned integer. UInt32.

Example

SELECT
    toUInt16(257) AS x,
    reinterpretAsUInt32(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsUInt64

Performs a byte-level reinterpretation of an input value as a 64-bit unsigned integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsUInt64(x)

Arguments

Returns

The byte-reinterpreted value as a 64-bit unsigned integer. UInt64.

Example

SELECT
    toUInt32(257) AS x,
    reinterpretAsUInt64(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsUInt128

Performs a byte-level reinterpretation of an input value as a 128-bit unsigned integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsUInt128(x)

Arguments

Returns

The byte-reinterpreted value as a 128-bit unsigned integer. UInt128.

Example

SELECT
    toUInt64(257) AS x,
    reinterpretAsUInt128(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsUInt256

Performs a byte-level reinterpretation of an input value as a 256-bit unsigned integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsUInt256(x)

Arguments

Returns

The byte-reinterpreted value as a 256-bit unsigned integer. UInt256.

Example

SELECT
    toUInt128(257) AS x,
    reinterpretAsUInt256(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsInt8

Performs a byte-level reinterpretation of an input value as an 8-bit signed integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsInt8(x)

Arguments

Returns

The byte-reinterpreted value as an 8-bit signed integer. Int8.

Example

SELECT
    toUInt8(257) AS x,
    reinterpretAsInt8(x) AS res

Result:

┌─x─┬─res─┐
│ 1 │   1 │
└───┴─────┘

reinterpretAsInt16

Performs a byte-level reinterpretation of an input value as a 16-bit signed integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsInt16(x)

Arguments

Returns

The byte-reinterpreted value as a 16-bit signed integer. Int16.

Example

SELECT
    toInt8(257) AS x,
    reinterpretAsInt16(x) AS res

Result:

┌─x─┬─res─┐
│ 1 │   1 │
└───┴─────┘

reinterpretAsInt32

Performs a byte-level reinterpretation of an input value as a 32-bit signed integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsInt32(x)

Arguments

Returns

The byte-reinterpreted value as a 32-bit signed integer. Int32.

Example

SELECT
    toInt16(257) AS x,
    reinterpretAsInt32(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsInt64

Performs a byte-level reinterpretation of an input value as a 64-bit signed integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsInt64(x)

Arguments

Returns

The byte-reinterpreted value as a 64-bit signed integer. Int64.

Example

SELECT
    toInt32(257) AS x,
    reinterpretAsInt64(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsInt128

Performs a byte-level reinterpretation of an input value as a 128-bit signed integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsInt128(x)

Arguments

Returns

The byte-reinterpreted value as a 128-bit signed integer. Int128.

Example

SELECT
    toInt64(257) AS x,
    reinterpretAsInt128(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsInt256

Performs a byte-level reinterpretation of an input value as a 256-bit signed integer. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsInt256(x)

Arguments

Returns

The byte-reinterpreted value as a 256-bit signed integer. Int256.

Example

SELECT
    toInt128(257) AS x,
    reinterpretAsInt256(x) AS res

Result:

┌───x─┬─res─┐
│ 257 │ 257 │
└─────┴─────┘

reinterpretAsFloat32

Performs a byte-level reinterpretation of an input value as a 32-bit floating-point number. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsFloat32(x)

Arguments

Returns

The byte-reinterpreted value as a 32-bit floating-point number. Float32.

Example

SELECT reinterpretAsUInt32(toFloat32(0.2)) as x, reinterpretAsFloat32(x)

Result:

┌──────────x─┬─reinterpretAsFloat32(x)─┐
│ 1045220557 │                     0.2 │
└────────────┴─────────────────────────┘

reinterpretAsFloat64

Performs a byte-level reinterpretation of an input value as a 64-bit floating-point number. This function does not attempt to preserve the original value's meaning.

Syntax

reinterpretAsFloat64(x)

Arguments

Returns

The byte-reinterpreted value as a 64-bit floating-point number. Float64.

Example

SELECT reinterpretAsUInt64(toFloat64(0.2)) as x, reinterpretAsFloat64(x)

Result:

┌───────────────────x─┬─reinterpretAsFloat64(x)─┐
│ 4596373779694328218 │                     0.2 │
└─────────────────────┴─────────────────────────┘

reinterpretAsDate

Interprets the bytes of an input value as a number (little-endian) and converts it to a Date representing days since the Unix Epoch.

Syntax

reinterpretAsDate(x)

Arguments

Returns

A date value. Date.

If the provided string isn’t long enough, the function works as if the string is padded with the necessary number of null bytes. If the string is longer than needed, the extra bytes are ignored.

Example

SELECT reinterpretAsDate(65), reinterpretAsDate('A')

Result:

┌─reinterpretAsDate(65)─┬─reinterpretAsDate('A')─┐
│            1970-03-07 │             1970-03-07 │
└───────────────────────┴────────────────────────┘

reinterpretAsDateTime

Interprets the bytes of an input value as a number (little-endian) and converts it to a DateTime representing seconds since the Unix Epoch.

Syntax

reinterpretAsDateTime(x)

Arguments

Returns

A datetime value. DateTime.

If the provided string isn’t long enough, the function works as if the string is padded with the necessary number of null bytes. If the string is longer than needed, the extra bytes are ignored.

Example

SELECT reinterpretAsDateTime(65), reinterpretAsDateTime('A')

Result:

┌─reinterpretAsDateTime(65)─┬─reinterpretAsDateTime('A')─┐
│       1970-01-01 01:01:05 │        1970-01-01 01:01:05 │
└───────────────────────────┴────────────────────────────┘

reinterpretAsString

Converts a numeric, date, or datetime value into a string by representing its underlying bytes in little-endian order. Null bytes are removed from the end of the resulting string.

Syntax

reinterpretAsString(x)

Arguments

  • x: The value to reinterpret as a string. Can be any numeric type, Date, or DateTime.

Returns

A string containing the byte representation of the input value. String.

Example

SELECT
    reinterpretAsString(toDateTime('1970-01-01 01:01:05')),
    reinterpretAsString(toDate('1970-03-07'))

Result:

┌─reinterpretAsString(toDateTime('1970-01-01 01:01:05'))─┬─reinterpretAsString(toDate('1970-03-07'))─┐
│ A                                                      │ A                                         │
└────────────────────────────────────────────────────────┴───────────────────────────────────────────┘

reinterpretAsFixedString

Converts a numeric, date, or datetime value into a fixed-length string by representing its underlying bytes in little-endian order. Null bytes are removed from the end of the resulting string.

Syntax

reinterpretAsFixedString(x)

Arguments

  • x: The value to reinterpret as a fixed string. Can be any numeric type, Date, or DateTime.

Returns

A fixed-length string containing the byte representation of the input value. FixedString.

Example

SELECT
    reinterpretAsFixedString(toDateTime('1970-01-01 01:01:05')),
    reinterpretAsFixedString(toDate('1970-03-07'))

Result:

┌─reinterpretAsFixedString(toDateTime('1970-01-01 01:01:05'))─┬─reinterpretAsFixedString(toDate('1970-03-07'))─┐
│ A                                                           │ A                                              │
└─────────────────────────────────────────────────────────────┴────────────────────────────────────────────────┘

reinterpretAsUUID

Interprets a 16-byte string as a UUID, treating each 8-byte half in little-endian byte order.

Syntax

reinterpretAsUUID(fixed_string)

Arguments

  • fixed_string: A 16-byte string to convert to a UUID. FixedString.

Returns

A UUID value. UUID.

In addition to the UUID functions listed here, there is dedicated UUID function documentation.

Example

SELECT reinterpretAsUUID(reverse(unhex('000102030405060708090a0b0c0d0e0f')))

Result:

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

reinterpret

Reinterprets the in-memory byte sequence of a value to a different data type without attempting to preserve its original logical value.

Syntax

reinterpret(x, type)

Arguments

  • x: The value to reinterpret. Can be of any data type.
  • type: A string specifying the target data type. String.

Returns

The value reinterpreted as the specified target type.

Example

SELECT reinterpret(toInt8(-1), 'UInt8') as int_to_uint,
    reinterpret(toInt8(1), 'Float32') as int_to_float,
    reinterpret('1', 'UInt32') as string_to_int

Result:

┌─int_to_uint─┬─int_to_float─┬─string_to_int─┐
│         255 │        1e-45 │            49 │
└─────────────┴──────────────┴───────────────┘

CAST

Converts an input value to a specified data type, attempting to preserve the value's meaning. If the conversion is not possible, an exception is raised.

Syntax

CAST(x, T)
CAST(x AS t)
x::t

Arguments

  • x: The value to convert. Can be of any data type.
  • T: A string specifying the target data type. String.
  • t: The target data type.

Returns

The converted value in the target data type.

If the input value doesn't fit the bounds of the target type, the result overflows. For example, CAST(-1, 'UInt8') returns 255.

Example

SELECT
    CAST(toInt8(-1), 'UInt8') AS cast_int_to_uint,
    CAST(1.5 AS Decimal(3,2)) AS cast_float_to_decimal,
    '1'::Int32 AS cast_string_to_int

Result:

┌─cast_int_to_uint─┬─cast_float_to_decimal─┬─cast_string_to_int─┐
│              255 │                  1.50 │                  1 │
└──────────────────┴───────────────────────┴────────────────────┘

accurateCast(x, T)

Converts a value x to the data type T, strictly enforcing type bounds. Unlike CAST, this function throws an exception if the value cannot be safely represented in the target type (e.g., due to overflow or underflow).

Syntax

accurateCast(x, T)

Arguments

  • x: The input value to convert.
  • T: A string specifying the target data type.

Returns

The converted value in the target data type.

Example

SELECT accurateCast(-1, 'UInt8') as uint8

Result:

Code: 70. DB::Exception: Received from localhost:9000. DB::Exception: Value in column Int8 can't be safely converted into type UInt8: While processing accurateCast(-1, 'UInt8') AS uint8.

accurateCastOrNull(x, T)

Converts a value x to the data type T, returning NULL if the value cannot be safely represented in the target type (e.g., due to overflow or underflow). The return type is always Nullable.

Syntax

accurateCastOrNull(x, T)

Arguments

  • x: The input value to convert.
  • T: A string specifying the target data type.

Returns

The converted value in the target data type if successful, otherwise NULL. Nullable of type T.

Example

SELECT
    accurateCastOrNull(-1, 'UInt8') as uint8,
    accurateCastOrNull(128, 'Int8') as int8,
    accurateCastOrNull('Test', 'FixedString(2)') as fixed_string

Result:

┌─uint8─┬─int8─┬─fixed_string─┐
│  ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ         │
└───────┴──────┴──────────────┘

accurateCastOrDefault(x, T[, default_value])

Converts a value x to the data type T, returning a specified default value if the value cannot be safely represented in the target type (e.g., due to overflow or underflow). If no default is provided, it returns the default value for type T.

Syntax

accurateCastOrDefault(x, T[, default_value])

Arguments

  • x: The input value to convert.
  • T: A string specifying the target data type.
  • default_value (optional): The value to return if the conversion is unsuccessful. This value must be compatible with type T.

Returns

The converted value in the target data type if successful, otherwise the default_value (if provided) or the default value for type T.

Example

SELECT
    accurateCastOrDefault(-1, 'UInt8') as uint8,
    accurateCastOrDefault(-1, 'UInt8', 5) as uint8_default,
    accurateCastOrDefault(128, 'Int8') as int8,
    accurateCastOrDefault(128, 'Int8', 5) as int8_default,
    accurateCastOrDefault('Test', 'FixedString(2)') as fixed_string,
    accurateCastOrDefault('Test', 'FixedString(2)', 'Te') as fixed_string_default

Result:

┌─uint8─┬─uint8_default─┬─int8─┬─int8_default─┬─fixed_string─┬─fixed_string_default─┐
│     0 │             5 │    0 │            5 │              │ Te                   │
└───────┴───────────────┴──────┴──────────────┴──────────────┴──────────────────────┘

toIntervalYear

Creates an interval representing a specified number of years.

Syntax

toIntervalYear(n)

Arguments

Returns

An interval of n years. IntervalYear.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalYear(1) AS interval_to_year
SELECT date + interval_to_year AS result

Result:

┌─────result─┐
│ 2025-06-15 │
└────────────┘

toIntervalQuarter

Creates an interval representing a specified number of quarters.

Syntax

toIntervalQuarter(n)

Arguments

Returns

An interval of n quarters. IntervalQuarter.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalQuarter(1) AS interval_to_quarter
SELECT date + interval_to_quarter AS result

Result:

┌─────result─┐
│ 2024-09-15 │
└────────────┘

toIntervalMonth

Creates an interval representing a specified number of months.

Syntax

toIntervalMonth(n)

Arguments

Returns

An interval of n months. IntervalMonth.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalMonth(1) AS interval_to_month
SELECT date + interval_to_month AS result

Result:

┌─────result─┐
│ 2024-07-15 │
└────────────┘

toIntervalWeek

Creates an interval representing a specified number of weeks.

Syntax

toIntervalWeek(n)

Arguments

Returns

An interval of n weeks. IntervalWeek.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalWeek(1) AS interval_to_week
SELECT date + interval_to_week AS result

Result:

┌─────result─┐
│ 2024-06-22 │
└────────────┘

toIntervalDay

Creates an interval representing a specified number of days.

Syntax

toIntervalDay(n)

Arguments

Returns

An interval of n days. IntervalDay.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalDay(5) AS interval_to_days
SELECT date + interval_to_days AS result

Result:

┌─────result─┐
│ 2024-06-20 │
└────────────┘

toIntervalHour

Creates an interval representing a specified number of hours.

Syntax

toIntervalHour(n)

Arguments

Returns

An interval of n hours. IntervalHour.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalHour(12) AS interval_to_hours
SELECT date + interval_to_hours AS result

Result:

┌──────────────result─┐
│ 2024-06-15 12:00:00 │
└─────────────────────┘

toIntervalMinute

Creates an interval representing a specified number of minutes.

Syntax

toIntervalMinute(n)

Arguments

Returns

An interval of n minutes. IntervalMinute.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalMinute(12) AS interval_to_minutes
SELECT date + interval_to_minutes AS result

Result:

┌──────────────result─┐
│ 2024-06-15 00:12:00 │
└─────────────────────┘

toIntervalSecond

Creates an interval representing a specified number of seconds.

Syntax

toIntervalSecond(n)

Arguments

Returns

An interval of n seconds. IntervalSecond.

Example

WITH
    toDate('2024-06-15') AS date,
    toIntervalSecond(30) AS interval_to_seconds
SELECT date + interval_to_seconds AS result

Result:

┌──────────────result─┐
│ 2024-06-15 00:00:30 │
└─────────────────────┘

toIntervalMillisecond

Creates an interval representing a specified number of milliseconds.

Syntax

toIntervalMillisecond(n)

Arguments

Returns

An interval of n milliseconds. IntervalMillisecond.

Example

WITH
    toDateTime('2024-06-15') AS date,
    toIntervalMillisecond(30) AS interval_to_milliseconds
SELECT date + interval_to_milliseconds AS result

Result:

┌──────────────────result─┐
│ 2024-06-15 00:00:00.030 │
└─────────────────────────┘

toIntervalMicrosecond

Creates an interval representing a specified number of microseconds.

Syntax

toIntervalMicrosecond(n)

Arguments

Returns

An interval of n microseconds. IntervalMicrosecond.

Example

WITH
    toDateTime('2024-06-15') AS date,
    toIntervalMicrosecond(30) AS interval_to_microseconds
SELECT date + interval_to_microseconds AS result

Result:

┌─────────────────────result─┐
│ 2024-06-15 00:00:00.000030 │
└────────────────────────────┘

toIntervalNanosecond

Creates an interval representing a specified number of nanoseconds.

Syntax

toIntervalNanosecond(n)

Arguments

Returns

An interval of n nanoseconds. IntervalNanosecond.

Example

WITH
    toDateTime('2024-06-15') AS date,
    toIntervalNanosecond(30) AS interval_to_nanoseconds
SELECT date + interval_to_nanoseconds AS result

Result:

┌────────────────────────result─┐
│ 2024-06-15 00:00:00.000000030 │
└───────────────────────────────┘

parseDateTime

Converts a string to a DateTime value using a MySQL-style format string. This is the inverse of formatDateTime.

Syntax

parseDateTime(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The MySQL-style format string. Defaults to '%Y-%m-%d %H:%i:%s'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime value parsed from the input string. DateTime.

Example

SELECT parseDateTime('2021-01-04+23:00:00', '%Y-%m-%d+%H:%i:%s')

Result:

┌─parseDateTime('2021-01-04+23:00:00', '%Y-%m-%d+%H:%i:%s')─┐
│                                       2021-01-04 23:00:00 │
└───────────────────────────────────────────────────────────┘

parseDateTimeOrZero

Converts a string to a DateTime value using a MySQL-style format string, returning the zero date (1970-01-01 00:00:00) if parsing fails.

Syntax

parseDateTimeOrZero(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The MySQL-style format string. Defaults to '%Y-%m-%d %H:%i:%s'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime value if parsing is successful, otherwise 1970-01-01 00:00:00. DateTime.

Example

SELECT parseDateTimeOrZero('2021-01-04+23:00:00', '%Y-%m-%d+%H:%i:%s'), parseDateTimeOrZero('invalid_date')

Result:

┌─parseDateTimeOrZero('2021-01-04+23:00:00', '%Y-%m-%d+%H:%i:%s')─┬─parseDateTimeOrZero('invalid_date')─┐
│                                       2021-01-04 23:00:00 │                   1970-01-01 00:00:00 │
└───────────────────────────────────────────────────────────┴───────────────────────────────────────┘

parseDateTimeOrNull

Converts a string to a DateTime value using a MySQL-style format string, returning NULL if parsing fails.

Syntax

parseDateTimeOrNull(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The MySQL-style format string. Defaults to '%Y-%m-%d %H:%i:%s'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime value if parsing is successful, otherwise NULL. Nullable(DateTime).

Example

SELECT parseDateTimeOrNull('2021-01-04+23:00:00', '%Y-%m-%d+%H:%i:%s'), parseDateTimeOrNull('invalid_date')

Result:

┌─parseDateTimeOrNull('2021-01-04+23:00:00', '%Y-%m-%d+%H:%i:%s')─┬─parseDateTimeOrNull('invalid_date')─┐
│                                       2021-01-04 23:00:00 │                                 ᴺᵁᴸᴸ │
└───────────────────────────────────────────────────────────┴───────────────────────────────────────┘

parseDateTimeInJodaSyntax

Converts a string to a DateTime value using a Joda-style format string. This is the inverse of formatDateTimeInJodaSyntax.

Syntax

parseDateTimeInJodaSyntax(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The Joda-style format string. Defaults to 'yyyy-MM-dd HH:mm:ss'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime value parsed from the input string. DateTime.

Example

SELECT parseDateTimeInJodaSyntax('2023-02-24 14:53:31', 'yyyy-MM-dd HH:mm:ss', 'Europe/Minsk')

Result:

┌─parseDateTimeInJodaSyntax('2023-02-24 14:53:31', 'yyyy-MM-dd HH:mm:ss', 'Europe/Minsk')─┐
│                                                                     2023-02-24 14:53:31 │
└─────────────────────────────────────────────────────────────────────────────────────────┘

parseDateTimeInJodaSyntaxOrZero

Converts a string to a DateTime value using a Joda-style format string, returning the zero date (1970-01-01 00:00:00) if parsing fails.

Syntax

parseDateTimeInJodaSyntaxOrZero(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The Joda-style format string. Defaults to 'yyyy-MM-dd HH:mm:ss'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime value if parsing is successful, otherwise 1970-01-01 00:00:00. DateTime.

Example

SELECT parseDateTimeInJodaSyntaxOrZero('invalid_date', 'yyyy-MM-dd HH:mm:ss', 'Europe/Minsk')

Result:

┌─parseDateTimeInJodaSyntaxOrZero('invalid_date', 'yyyy-MM-dd HH:mm:ss', 'Europe/Minsk')─┐
│                                                                     1970-01-01 00:00:00 │
└─────────────────────────────────────────────────────────────────────────────────────────┘

parseDateTimeInJodaSyntaxOrNull

Converts a string to a DateTime value using a Joda-style format string, returning NULL if parsing fails.

Syntax

parseDateTimeInJodaSyntaxOrNull(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The Joda-style format string. Defaults to 'yyyy-MM-dd HH:mm:ss'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime value if parsing is successful, otherwise NULL. Nullable(DateTime).

Example

SELECT parseDateTimeInJodaSyntaxOrNull('invalid_date', 'yyyy-MM-dd HH:mm:ss', 'Europe/Minsk')

Result:

┌─parseDateTimeInJodaSyntaxOrNull('invalid_date', 'yyyy-MM-dd HH:mm:ss', 'Europe/Minsk')─┐
│                                                                                     ᴺᵁᴸᴸ │
└─────────────────────────────────────────────────────────────────────────────────────────┘

parseDateTime64

Converts a string to a DateTime64 value using a MySQL-style format string.

Syntax

parseDateTime64(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The MySQL-style format string. Defaults to '%Y-%m-%d %H:%i:%s.%f'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime64 value parsed from the input string with a precision of 6. DateTime64.

Example

SELECT parseDateTime64('2021-01-04 23:00:00.123456', '%Y-%m-%d %H:%i:%s.%f')

Result:

┌─parseDateTime64('2021-01-04 23:00:00.123456', '%Y-%m-%d %H:%i:%s.%f')─┐
│                                          2021-01-04 23:00:00.123456 │
└──────────────────────────────────────────────────────────────────────┘

parseDateTime64OrZero

Converts a string to a DateTime64 value using a MySQL-style format string, returning the zero date (1970-01-01 00:00:00.000000) if parsing fails.

Syntax

parseDateTime64OrZero(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The MySQL-style format string. Defaults to '%Y-%m-%d %H:%i:%s.%f'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime64 value if parsing is successful, otherwise 1970-01-01 00:00:00.000000. DateTime64.

Example

SELECT parseDateTime64OrZero('invalid_date', '%Y-%m-%d %H:%i:%s.%f')

Result:

┌─parseDateTime64OrZero('invalid_date', '%Y-%m-%d %H:%i:%s.%f')─┐
│                                          1970-01-01 00:00:00.000000 │
└──────────────────────────────────────────────────────────────────────┘

parseDateTime64OrNull

Converts a string to a DateTime64 value using a MySQL-style format string, returning NULL if parsing fails.

Syntax

parseDateTime64OrNull(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The MySQL-style format string. Defaults to '%Y-%m-%d %H:%i:%s.%f'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime64 value if parsing is successful, otherwise NULL. Nullable(DateTime64).

Example

SELECT parseDateTime64OrNull('invalid_date', '%Y-%m-%d %H:%i:%s.%f')

Result:

┌─parseDateTime64OrNull('invalid_date', '%Y-%m-%d %H:%i:%s.%f')─┐
│                                                          ᴺᵁᴸᴸ │
└────────────────────────────────────────────────────────────────┘

parseDateTime64InJodaSyntax

Converts a string to a DateTime64 value using a Joda-style format string.

Syntax

parseDateTime64InJodaSyntax(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The Joda-style format string. Defaults to 'yyyy-MM-dd HH:mm:ss'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime64 value parsed from the input string. The precision is determined by the number of 'S' placeholders in the format string (up to 6). DateTime64.

Example

SELECT parseDateTime64InJodaSyntax('2023-02-24 14:53:31.123', 'yyyy-MM-dd HH:mm:ss.SSS', 'Europe/Minsk')

Result:

┌─parseDateTime64InJodaSyntax('2023-02-24 14:53:31.123', 'yyyy-MM-dd HH:mm:ss.SSS', 'Europe/Minsk')─┐
│                                                                     2023-02-24 14:53:31.123 │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

parseDateTime64InJodaSyntaxOrZero

Converts a string to a DateTime64 value using a Joda-style format string, returning the zero date (1970-01-01 00:00:00.000000) if parsing fails.

Syntax

parseDateTime64InJodaSyntaxOrZero(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The Joda-style format string. Defaults to 'yyyy-MM-dd HH:mm:ss'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime64 value if parsing is successful, otherwise 1970-01-01 00:00:00.000000. DateTime64.

Example

SELECT parseDateTime64InJodaSyntaxOrZero('invalid_date', 'yyyy-MM-dd HH:mm:ss.SSS', 'Europe/Minsk')

Result:

┌─parseDateTime64InJodaSyntaxOrZero('invalid_date', 'yyyy-MM-dd HH:mm:ss.SSS', 'Europe/Minsk')─┐
│                                                                     1970-01-01 00:00:00.000 │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

parseDateTime64InJodaSyntaxOrNull

Converts a string to a DateTime64 value using a Joda-style format string, returning NULL if parsing fails.

Syntax

parseDateTime64InJodaSyntaxOrNull(str[, format[, timezone]])

Arguments

  • str: The string to parse. String.
  • format (optional): The Joda-style format string. Defaults to 'yyyy-MM-dd HH:mm:ss'.
  • timezone (optional): The timezone to use for parsing. String.

Returns

A DateTime64 value if parsing is successful, otherwise NULL. Nullable(DateTime64).

Example

SELECT parseDateTime64InJodaSyntaxOrNull('invalid_date', 'yyyy-MM-dd HH:mm:ss.SSS', 'Europe/Minsk')

Result:

┌─parseDateTime64InJodaSyntaxOrNull('invalid_date', 'yyyy-MM-dd HH:mm:ss.SSS', 'Europe/Minsk')─┐
│                                                                                         ᴺᵁᴸᴸ │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

parseDateTimeBestEffort

Converts a string representation of a date and time to a DateTime data type, attempting to parse various common formats.

Syntax

parseDateTimeBestEffort(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value converted from the input string. DateTime.

Example

SELECT
    parseDateTimeBestEffort('23/10/2020 12:12:57') AS from_ddmmyyyy_hhmmss,
    parseDateTimeBestEffort('Sat, 18 Aug 2018 07:22:16 GMT', 'Asia/Istanbul') AS from_rfc_with_tz,
    parseDateTimeBestEffort('1284101485') AS from_unix_timestamp_string

Result:

┌─from_ddmmyyyy_hhmmss─┬─from_rfc_with_tz─┬─from_unix_timestamp_string─┐
│ 2020-10-23 12:12:57  │ 2018-08-18 10:22:16 │ 2015-07-07 12:04:41        │
└──────────────────────┴───────────────────┴────────────────────────────┘

parseDateTime32BestEffort

Alias for parseDateTimeBestEffort.

Syntax

parseDateTime32BestEffort(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value converted from the input string. DateTime.

Example

SELECT parseDateTime32BestEffort('2018-10-23 10:12:12') AS result

Result:

┌─result─┐
│ 2018-10-23 10:12:12 │
└────────┘

parseDateTimeBestEffortUS

Converts a string representation of a date and time to a DateTime data type, prioritizing US date formats (MM/DD/YYYY) when ambiguity exists.

Syntax

parseDateTimeBestEffortUS(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value converted from the input string. DateTime.

Example

SELECT parseDateTimeBestEffortUS('01/02/2023 10:00:00') AS us_format_date

Result:

┌─us_format_date─┐
│ 2023-01-02 10:00:00 │
└────────────────┘

parseDateTimeBestEffortOrNull

Converts a string representation of a date and time to a DateTime data type, returning NULL if parsing fails.

Syntax

parseDateTimeBestEffortOrNull(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value if parsing is successful, otherwise NULL. Nullable(DateTime).

Example

SELECT parseDateTimeBestEffortOrNull('invalid_date_string') AS result

Result:

┌─result─┐
│   ᴺᵁᴸᴸ │
└────────┘

parseDateTime32BestEffortOrNull

Alias for parseDateTimeBestEffortOrNull.

Syntax

parseDateTime32BestEffortOrNull(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value if parsing is successful, otherwise NULL. Nullable(DateTime).

Example

SELECT parseDateTime32BestEffortOrNull('another_bad_date') AS result

Result:

┌─result─┐
│   ᴺᵁᴸᴸ │
└────────┘

parseDateTimeBestEffortOrZero

Converts a string representation of a date and time to a DateTime data type, returning the zero date (1970-01-01 00:00:00) if parsing fails.

Syntax

parseDateTimeBestEffortOrZero(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value if parsing is successful, otherwise 1970-01-01 00:00:00. DateTime.

Example

SELECT parseDateTimeBestEffortOrZero('invalid_date_string') AS result

Result:

┌─result─┐
│ 1970-01-01 00:00:00 │
└────────┘

parseDateTime32BestEffortOrZero

Alias for parseDateTimeBestEffortOrZero.

Syntax

parseDateTime32BestEffortOrZero(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value if parsing is successful, otherwise 1970-01-01 00:00:00. DateTime.

Example

SELECT parseDateTime32BestEffortOrZero('another_bad_date') AS result

Result:

┌─result─┐
│ 1970-01-01 00:00:00 │
└────────┘

parseDateTimeBestEffortUSOrNull

Converts a string representation of a date and time to a DateTime data type, prioritizing US date formats (MM/DD/YYYY) when ambiguity exists, and returning NULL if parsing fails.

Syntax

parseDateTimeBestEffortUSOrNull(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value if parsing is successful, otherwise NULL. Nullable(DateTime).

Example

SELECT parseDateTimeBestEffortUSOrNull('invalid_date_string') AS result

Result:

┌─result─┐
│   ᴺᵁᴸᴸ │
└────────┘

parseDateTimeBestEffortUSOrZero

Converts a string representation of a date and time to a DateTime data type, prioritizing US date formats (MM/DD/YYYY) when ambiguity exists, and returning the zero date (1970-01-01 00:00:00) if parsing fails.

Syntax

parseDateTimeBestEffortUSOrZero(time_string [, time_zone])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime value if parsing is successful, otherwise 1970-01-01 00:00:00. DateTime.

Example

SELECT parseDateTimeBestEffortUSOrZero('invalid_date_string') AS result

Result:

┌─result─┐
│ 1970-01-01 00:00:00 │
└────────┘

parseDateTime64BestEffort

Converts a string representation of a date and time to a DateTime64 data type, attempting to parse various common formats including sub-second precision.

Syntax

parseDateTime64BestEffort(time_string [, precision [, time_zone]])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • precision (optional): The desired sub-second precision (e.g., 3 for milliseconds, 6 for microseconds). Defaults to 3. UInt8.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime64 value converted from the input string. DateTime64.

Example

SELECT parseDateTime64BestEffort('2021-01-01 01:01:00.12346', 6, 'Asia/Istanbul') AS result

Result:

┌─────────────────────result─┐
│ 2020-12-31 22:01:00.123460 │
└────────────────────────────┘

parseDateTime64BestEffortUS

Converts a string representation of a date and time to a DateTime64 data type, prioritizing US date formats (MM/DD/YYYY) when ambiguity exists, and including sub-second precision.

Syntax

parseDateTime64BestEffortUS(time_string [, precision [, time_zone]])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • precision (optional): The desired sub-second precision (e.g., 3 for milliseconds, 6 for microseconds). Defaults to 3. UInt8.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime64 value converted from the input string. DateTime64.

Example

SELECT parseDateTime64BestEffortUS('01/02/2023 10:00:00.123', 3) AS us_format_datetime

Result:

┌─us_format_datetime─┐
│ 2023-01-02 10:00:00.123 │
└────────────────────┘

parseDateTime64BestEffortOrNull

Converts a string representation of a date and time to a DateTime64 data type, returning NULL if parsing fails.

Syntax

parseDateTime64BestEffortOrNull(time_string [, precision [, time_zone]])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • precision (optional): The desired sub-second precision. UInt8.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime64 value if parsing is successful, otherwise NULL. Nullable(DateTime64).

Example

SELECT parseDateTime64BestEffortOrNull('invalid_datetime_string', 3) AS result

Result:

┌─result─┐
│   ᴺᵁᴸᴸ │
└────────┘

parseDateTime64BestEffortOrZero

Converts a string representation of a date and time to a DateTime64 data type, returning the zero date (1970-01-01 00:00:00.000000) if parsing fails.

Syntax

parseDateTime64BestEffortOrZero(time_string [, precision [, time_zone]])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • precision (optional): The desired sub-second precision. UInt8.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime64 value if parsing is successful, otherwise 1970-01-01 00:00:00.000000. DateTime64.

Example

SELECT parseDateTime64BestEffortOrZero('invalid_datetime_string', 3) AS result

Result:

┌─result─┐
│ 1970-01-01 00:00:00.000 │
└────────┘

parseDateTime64BestEffortUSOrNull

Converts a string representation of a date and time to a DateTime64 data type, prioritizing US date formats (MM/DD/YYYY) when ambiguity exists, and returning NULL if parsing fails.

Syntax

parseDateTime64BestEffortUSOrNull(time_string [, precision [, time_zone]])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • precision (optional): The desired sub-second precision. UInt8.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime64 value if parsing is successful, otherwise NULL. Nullable(DateTime64).

Example

SELECT parseDateTime64BestEffortUSOrNull('invalid_datetime_string', 3) AS result

Result:

┌─result─┐
│   ᴺᵁᴸᴸ │
└────────┘

parseDateTime64BestEffortUSOrZero

Converts a string representation of a date and time to a DateTime64 data type, prioritizing US date formats (MM/DD/YYYY) when ambiguity exists, and returning the zero date (1970-01-01 00:00:00.000000) if parsing fails.

Syntax

parseDateTime64BestEffortUSOrZero(time_string [, precision [, time_zone]])

Arguments

  • time_string: The string containing the date and time to convert. String.
  • precision (optional): The desired sub-second precision. UInt8.
  • time_zone (optional): The timezone to use for parsing the time_string. String.

Returns

A DateTime64 value if parsing is successful, otherwise 1970-01-01 00:00:00.000000. DateTime64.

Example

SELECT parseDateTime64BestEffortUSOrZero('invalid_datetime_string', 3) AS result

Result:

┌─result─┐
│ 1970-01-01 00:00:00.000 │
└────────┘

toLowCardinality

Converts an input value to its LowCardinality version, which can optimize storage and query performance for columns with a limited number of distinct values.

Syntax

toLowCardinality(expr)

Arguments

Returns

The input value converted to a LowCardinality type. LowCardinality.

Example

SELECT toLowCardinality('1')

Result:

┌─toLowCardinality('1')─┐
│ 1                     │
└───────────────────────┘

toUnixTimestamp64Second

Converts a DateTime64 value to an Int64 Unix timestamp with second precision. The input DateTime64 value is scaled appropriately.

Syntax

toUnixTimestamp64Second(value)

Arguments

  • value: A DateTime64 value with any precision. DateTime64.

Returns

An Int64 Unix timestamp representing seconds since the Unix Epoch in UTC. Int64.

Example

WITH toDateTime64('2009-02-13 23:31:31.011', 3, 'UTC') AS dt64
SELECT toUnixTimestamp64Second(dt64)

Result:

┌─toUnixTimestamp64Second(dt64)─┐
│                    1234567891 │
└───────────────────────────────┘

toUnixTimestamp64Milli

Converts a DateTime64 value to an Int64 Unix timestamp with millisecond precision. The input DateTime64 value is scaled appropriately.

Syntax

toUnixTimestamp64Milli(value)

Arguments

  • value: A DateTime64 value with any precision. DateTime64.

Returns

An Int64 Unix timestamp representing milliseconds since the Unix Epoch in UTC. Int64.

Example

WITH toDateTime64('2009-02-13 23:31:31.011', 3, 'UTC') AS dt64
SELECT toUnixTimestamp64Milli(dt64)

Result:

┌─toUnixTimestamp64Milli(dt64)─┐
│                1234567891011 │
└──────────────────────────────┘

toUnixTimestamp64Micro

Converts a DateTime64 value to an Int64 Unix timestamp with microsecond precision. The input DateTime64 value is scaled appropriately.

Syntax

toUnixTimestamp64Micro(value)

Arguments

  • value: A DateTime64 value with any precision. DateTime64.

Returns

An Int64 Unix timestamp representing microseconds since the Unix Epoch in UTC. Int64.

Example

WITH toDateTime64('1970-01-15 06:56:07.891011', 6, 'UTC') AS dt64
SELECT toUnixTimestamp64Micro(dt64)

Result:

┌─toUnixTimestamp64Micro(dt64)─┐
│                1234567891011 │
└──────────────────────────────┘

toUnixTimestamp64Nano

Converts a DateTime64 value to an Int64 Unix timestamp with nanosecond precision. The input DateTime64 value is scaled appropriately.

Syntax

toUnixTimestamp64Nano(value)

Arguments

  • value: A DateTime64 value with any precision. DateTime64.

Returns

An Int64 Unix timestamp representing nanoseconds since the Unix Epoch in UTC. Int64.

Example

WITH toDateTime64('1970-01-01 00:20:34.567891011', 9, 'UTC') AS dt64
SELECT toUnixTimestamp64Nano(dt64)

Result:

┌─toUnixTimestamp64Nano(dt64)─┐
│               1234567891011 │
└─────────────────────────────┘

fromUnixTimestamp64Second

Converts an Int64 Unix timestamp (seconds) to a DateTime64 value with second precision, optionally specifying a timezone.

Syntax

fromUnixTimestamp64Second(value[, timezone])

Arguments

  • value: An Int64 Unix timestamp representing seconds since the Unix Epoch in UTC. Int64.
  • timezone (optional): A string specifying the timezone for the resulting DateTime64 value. String.

Returns

A DateTime64 value with 0 precision (seconds). DateTime64.

Example

WITH CAST(1733935988, 'Int64') AS i64
SELECT
    fromUnixTimestamp64Second(i64, 'UTC') AS x,
    toTypeName(x)

Result:

┌───────────────────x─┬─toTypeName(x)────────┐
│ 2024-12-11 16:53:08 │ DateTime64(0, 'UTC') │
└─────────────────────┴──────────────────────┘

fromUnixTimestamp64Milli

Converts an Int64 Unix timestamp (milliseconds) to a DateTime64 value with millisecond precision, optionally specifying a timezone.

Syntax

fromUnixTimestamp64Milli(value[, timezone])

Arguments

  • value: An Int64 Unix timestamp representing milliseconds since the Unix Epoch in UTC. Int64.
  • timezone (optional): A string specifying the timezone for the resulting DateTime64 value. String.

Returns

A DateTime64 value with 3 precision (milliseconds). DateTime64.

Example

WITH CAST(1733935988123, 'Int64') AS i64
SELECT
    fromUnixTimestamp64Milli(i64, 'UTC') AS x,
    toTypeName(x)

Result:

┌───────────────────────x─┬─toTypeName(x)────────┐
│ 2024-12-11 16:53:08.123 │ DateTime64(3, 'UTC') │
└─────────────────────────┴──────────────────────┘

fromUnixTimestamp64Micro

Converts an Int64 Unix timestamp (microseconds) to a DateTime64 value with microsecond precision, optionally specifying a timezone.

Syntax

fromUnixTimestamp64Micro(value[, timezone])

Arguments

  • value: An Int64 Unix timestamp representing microseconds since the Unix Epoch in UTC. Int64.
  • timezone (optional): A string specifying the timezone for the resulting DateTime64 value. String.

Returns

A DateTime64 value with 6 precision (microseconds). DateTime64.

Example

WITH CAST(1733935988123456, 'Int64') AS i64
SELECT
    fromUnixTimestamp64Micro(i64, 'UTC') AS x,
    toTypeName(x)

Result:

┌──────────────────────────x─┬─toTypeName(x)────────┐
│ 2024-12-11 16:53:08.123456 │ DateTime64(6, 'UTC') │
└────────────────────────────┴──────────────────────┘

fromUnixTimestamp64Nano

Converts an Int64 Unix timestamp (nanoseconds) to a DateTime64 value with nanosecond precision, optionally specifying a timezone.

Syntax

fromUnixTimestamp64Nano(value[, timezone])

Arguments

  • value: An Int64 Unix timestamp representing nanoseconds since the Unix Epoch in UTC. Int64.
  • timezone (optional): A string specifying the timezone for the resulting DateTime64 value. String.

Returns

A DateTime64 value with 9 precision (nanoseconds). DateTime64.

Example

WITH CAST(1733935988123456789, 'Int64') AS i64
SELECT
    fromUnixTimestamp64Nano(i64, 'UTC') AS x,
    toTypeName(x)

Result:

┌─────────────────────────────x─┬─toTypeName(x)────────┐
│ 2024-12-11 16:53:08.123456789 │ DateTime64(9, 'UTC') │
└───────────────────────────────┴──────────────────────┘

formatRow

Formats multiple expressions into a single string using a specified text format. The output typically includes a newline character at the end.

Syntax

formatRow(format, x, y, ...)

Arguments

  • format: The name of the text format to use (e.g., 'CSV', 'TSV').
  • x, y, ...: The expressions to format.

Returns

A formatted string, usually terminated with a newline character. String.

Example

SELECT formatRow('CSV', number, 'good')
FROM numbers(3)

Result:

┌─formatRow('CSV', number, 'good')─┐
│ 0,"good"
                         │
│ 1,"good"
                         │
│ 2,"good"
                         │
└──────────────────────────────────┘

formatRowNoNewline

Formats multiple expressions into a single string using a specified text format, similar to formatRow, but removes any trailing newline character.

Syntax

formatRowNoNewline(format, x, y, ...)

Arguments

  • format: The name of the text format to use (e.g., 'CSV', 'TSV').
  • x, y, ...: The expressions to format.

Returns

A formatted string, without a trailing newline character. String.

Example

SELECT formatRowNoNewline('CSV', number, 'good')
FROM numbers(3)

Result:

┌─formatRowNoNewline('CSV', number, 'good')─┐
│ 0,"good"                                  │
│ 1,"good"                                  │
│ 2,"good"                                  │
└───────────────────────────────────────────┘
Updated