Type Conversion Functions
Common Issues with Data Conversion
ClickHouse generally uses the same behavior as C++ programs.
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 is not 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 value of type Bool
. Throws an exception in case of an error.
Syntax
toBool(expr)
Arguments
expr
— Expression returning a number or a string. Expression.
Supported arguments:
- Values of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
- Strings
true
orfalse
(case-insensitive).
Returned value
- Returns
true
orfalse
based on evaluation of the argument. Bool.
Example
Query:
SELECT
toBool(toUInt8(1)),
toBool(toInt8(-1)),
toBool(toFloat32(1.01)),
toBool('true'),
toBool('false'),
toBool('FALSE')
FORMAT Vertical
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 a value of type Int8
. Throws an exception in case of an error.
Syntax
toInt8(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt8('0xc0fe');
.
If the input value cannot be represented within the bounds of Int8, overflow or underflow of the result occurs.
This is not considered an error.
For example: SELECT toInt8(128) == -128;
.
Returned value
- 8-bit integer value. Int8.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt8(-8),
toInt8(-8.8),
toInt8('-8')
FORMAT Vertical;
Result:
Row 1:
──────
toInt8(-8): -8
toInt8(-8.8): -8
toInt8('-8'): -8
See also
toInt8OrZero
Like toInt8
, this function converts an input value to a value of type Int8 but returns 0
in case of an error.
Syntax
toInt8OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of ordinary Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt8OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of Int8, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 8-bit integer value if successful, otherwise
0
. Int8.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt8OrZero('-8'),
toInt8OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt8OrZero('-8'): -8
toInt8OrZero('abc'): 0
See also
toInt8OrNull
Like toInt8
, this function converts an input value to a value of type Int8 but returns NULL
in case of an error.
Syntax
toInt8OrNull(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt8OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of Int8, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt8OrNull('-8'),
toInt8OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt8OrNull('-8'): -8
toInt8OrNull('abc'): ᴺᵁᴸᴸ
See also
toInt8OrDefault
Like toInt8
, this function converts an input value to a value of type Int8 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toInt8OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeInt8
is unsuccessful. Int8.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt8OrDefault('0xc0fe', CAST('-1', 'Int8'));
.
If the input value cannot be represented within the bounds of Int8, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 8-bit integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toInt8OrDefault('-8', CAST('-1', 'Int8')),
toInt8OrDefault('abc', CAST('-1', 'Int8'))
FORMAT Vertical;
Result:
Row 1:
──────
toInt8OrDefault('-8', CAST('-1', 'Int8')): -8
toInt8OrDefault('abc', CAST('-1', 'Int8')): -1
See also
toInt16
Converts an input value to a value of type Int16
. Throws an exception in case of an error.
Syntax
toInt16(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt16('0xc0fe');
.
If the input value cannot be represented within the bounds of Int16, overflow or underflow of the result occurs.
This is not considered an error.
For example: SELECT toInt16(32768) == -32768;
.
Returned value
- 16-bit integer value. Int16.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt16(-16),
toInt16(-16.16),
toInt16('-16')
FORMAT Vertical;
Result:
Row 1:
──────
toInt16(-16): -16
toInt16(-16.16): -16
toInt16('-16'): -16
See also
toInt16OrZero
Like toInt16
, this function converts an input value to a value of type Int16 but returns 0
in case of an error.
Syntax
toInt16OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt16OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of Int16, overflow or underflow of the result occurs. This is not considered as an error.
Returned value
- 16-bit integer value if successful, otherwise
0
. Int16.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt16OrZero('-16'),
toInt16OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt16OrZero('-16'): -16
toInt16OrZero('abc'): 0
See also
toInt16OrNull
Like toInt16
, this function converts an input value to a value of type Int16 but returns NULL
in case of an error.
Syntax
toInt16OrNull(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt16OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of Int16, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt16OrNull('-16'),
toInt16OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt16OrNull('-16'): -16
toInt16OrNull('abc'): ᴺᵁᴸᴸ
See also
toInt16OrDefault
Like toInt16
, this function converts an input value to a value of type Int16 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toInt16OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeInt16
is unsuccessful. Int16.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt16OrDefault('0xc0fe', CAST('-1', 'Int16'));
.
If the input value cannot be represented within the bounds of Int16, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 16-bit integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toInt16OrDefault('-16', CAST('-1', 'Int16')),
toInt16OrDefault('abc', CAST('-1', 'Int16'))
FORMAT Vertical;
Result:
Row 1:
──────
toInt16OrDefault('-16', CAST('-1', 'Int16')): -16
toInt16OrDefault('abc', CAST('-1', 'Int16')): -1
See also
toInt32
Converts an input value to a value of type Int32
. Throws an exception in case of an error.
Syntax
toInt32(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt32('0xc0fe');
.
If the input value cannot be represented within the bounds of Int32, the result over or under flows.
This is not considered an error.
For example: SELECT toInt32(2147483648) == -2147483648;
Returned value
- 32-bit integer value. Int32.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt32(-32),
toInt32(-32.32),
toInt32('-32')
FORMAT Vertical;
Result:
Row 1:
──────
toInt32(-32): -32
toInt32(-32.32): -32
toInt32('-32'): -32
See also
toInt32OrZero
Like toInt32
, this function converts an input value to a value of type Int32 but returns 0
in case of an error.
Syntax
toInt32OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt32OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of Int32, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 32-bit integer value if successful, otherwise
0
. Int32
The function uses rounding towards zero, meaning it truncate fractional digits of numbers.
Example
Query:
SELECT
toInt32OrZero('-32'),
toInt32OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt32OrZero('-32'): -32
toInt32OrZero('abc'): 0
See also
toInt32OrNull
Like toInt32
, this function converts an input value to a value of type Int32 but returns NULL
in case of an error.
Syntax
toInt32OrNull(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt32OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of Int32, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt32OrNull('-32'),
toInt32OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt32OrNull('-32'): -32
toInt32OrNull('abc'): ᴺᵁᴸᴸ
See also
toInt32OrDefault
Like toInt32
, this function converts an input value to a value of type Int32 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toInt32OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeInt32
is unsuccessful. Int32.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt32OrDefault('0xc0fe', CAST('-1', 'Int32'));
.
If the input value cannot be represented within the bounds of Int32, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 32-bit integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toInt32OrDefault('-32', CAST('-1', 'Int32')),
toInt32OrDefault('abc', CAST('-1', 'Int32'))
FORMAT Vertical;
Result:
Row 1:
──────
toInt32OrDefault('-32', CAST('-1', 'Int32')): -32
toInt32OrDefault('abc', CAST('-1', 'Int32')): -1
See also
toInt64
Converts an input value to a value of type Int64
. Throws an exception in case of an error.
Syntax
toInt64(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported types:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt64('0xc0fe');
.
If the input value cannot be represented within the bounds of Int64, the result over or under flows.
This is not considered an error.
For example: SELECT toInt64(9223372036854775808) == -9223372036854775808;
Returned value
- 64-bit integer value. Int64.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt64(-64),
toInt64(-64.64),
toInt64('-64')
FORMAT Vertical;
Result:
Row 1:
──────
toInt64(-64): -64
toInt64(-64.64): -64
toInt64('-64'): -64
See also
toInt64OrZero
Like toInt64
, this function converts an input value to a value of type Int64 but returns 0
in case of an error.
Syntax
toInt64OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt64OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of Int64, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 64-bit integer value if successful, otherwise
0
. Int64.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt64OrZero('-64'),
toInt64OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt64OrZero('-64'): -64
toInt64OrZero('abc'): 0
See also
toInt64OrNull
Like toInt64
, this function converts an input value to a value of type Int64 but returns NULL
in case of an error.
Syntax
toInt64OrNull(x)
Arguments
x
— A String representation of a number. Expression / String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt64OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of Int64, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt64OrNull('-64'),
toInt64OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt64OrNull('-64'): -64
toInt64OrNull('abc'): ᴺᵁᴸᴸ
See also
toInt64OrDefault
Like toInt64
, this function converts an input value to a value of type Int64 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toInt64OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeInt64
is unsuccessful. Int64.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt64OrDefault('0xc0fe', CAST('-1', 'Int64'));
.
If the input value cannot be represented within the bounds of Int64, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 64-bit integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toInt64OrDefault('-64', CAST('-1', 'Int64')),
toInt64OrDefault('abc', CAST('-1', 'Int64'))
FORMAT Vertical;
Result:
Row 1:
──────
toInt64OrDefault('-64', CAST('-1', 'Int64')): -64
toInt64OrDefault('abc', CAST('-1', 'Int64')): -1
See also
toInt128
Converts an input value to a value of type Int128
. Throws an exception in case of an error.
Syntax
toInt128(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt128('0xc0fe');
.
If the input value cannot be represented within the bounds of Int128, the result over or under flows. This is not considered an error.
Returned value
- 128-bit integer value. Int128.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt128(-128),
toInt128(-128.8),
toInt128('-128')
FORMAT Vertical;
Result:
Row 1:
──────
toInt128(-128): -128
toInt128(-128.8): -128
toInt128('-128'): -128
See also
toInt128OrZero
Like toInt128
, this function converts an input value to a value of type Int128 but returns 0
in case of an error.
Syntax
toInt128OrZero(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt128OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of Int128, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 128-bit integer value if successful, otherwise
0
. Int128.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt128OrZero('-128'),
toInt128OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt128OrZero('-128'): -128
toInt128OrZero('abc'): 0
See also
toInt128OrNull
Like toInt128
, this function converts an input value to a value of type Int128 but returns NULL
in case of an error.
Syntax
toInt128OrNull(x)
Arguments
x
— A String representation of a number. Expression / String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt128OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of Int128, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt128OrNull('-128'),
toInt128OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt128OrNull('-128'): -128
toInt128OrNull('abc'): ᴺᵁᴸᴸ
See also
toInt128OrDefault
Like toInt128
, this function converts an input value to a value of type Int128 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toInt128OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeInt128
is unsuccessful. Int128.
Supported arguments:
- (U)Int8/16/32/64/128/256.
- Float32/64.
- String representations of (U)Int8/16/32/128/256.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt128OrDefault('0xc0fe', CAST('-1', 'Int128'));
.
If the input value cannot be represented within the bounds of Int128, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 128-bit integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toInt128OrDefault('-128', CAST('-1', 'Int128')),
toInt128OrDefault('abc', CAST('-1', 'Int128'))
FORMAT Vertical;
Result:
Row 1:
──────
toInt128OrDefault('-128', CAST('-1', 'Int128')): -128
toInt128OrDefault('abc', CAST('-1', 'Int128')): -1
See also
toInt256
Converts an input value to a value of type Int256
. Throws an exception in case of an error.
Syntax
toInt256(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt256('0xc0fe');
.
If the input value cannot be represented within the bounds of Int256, the result over or under flows. This is not considered an error.
Returned value
- 256-bit integer value. Int256.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt256(-256),
toInt256(-256.256),
toInt256('-256')
FORMAT Vertical;
Result:
Row 1:
──────
toInt256(-256): -256
toInt256(-256.256): -256
toInt256('-256'): -256
See also
toInt256OrZero
Like toInt256
, this function converts an input value to a value of type Int256 but returns 0
in case of an error.
Syntax
toInt256OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt256OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of Int256, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 256-bit integer value if successful, otherwise
0
. Int256.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt256OrZero('-256'),
toInt256OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt256OrZero('-256'): -256
toInt256OrZero('abc'): 0
See also
toInt256OrNull
Like toInt256
, this function converts an input value to a value of type Int256 but returns NULL
in case of an error.
Syntax
toInt256OrNull(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toInt256OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of Int256, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toInt256OrNull('-256'),
toInt256OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toInt256OrNull('-256'): -256
toInt256OrNull('abc'): ᴺᵁᴸᴸ
See also
toInt256OrDefault
Like toInt256
, this function converts an input value to a value of type Int256 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toInt256OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeInt256
is unsuccessful. Int256.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
- String representations of binary and hexadecimal values, e.g.
SELECT toInt256OrDefault('0xc0fe', CAST('-1', 'Int256'));
If the input value cannot be represented within the bounds of Int256, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 256-bit integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toInt256OrDefault('-256', CAST('-1', 'Int256')),
toInt256OrDefault('abc', CAST('-1', 'Int256'))
FORMAT Vertical;
Result:
Row 1:
──────
toInt256OrDefault('-256', CAST('-1', 'Int256')): -256
toInt256OrDefault('abc', CAST('-1', 'Int256')): -1
See also
toUInt8
Converts an input value to a value of type UInt8
. Throws an exception in case of an error.
Syntax
toUInt8(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt8('0xc0fe');
.
If the input value cannot be represented within the bounds of UInt8, overflow or underflow of the result occurs.
This is not considered an error.
For example: SELECT toUInt8(256) == 0;
.
Returned value
- 8-bit unsigned integer value. UInt8.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toUInt8(8),
toUInt8(8.8),
toUInt8('8')
FORMAT Vertical;
Result:
Row 1:
──────
toUInt8(8): 8
toUInt8(8.8): 8
toUInt8('8'): 8
See also
toUInt8OrZero
Like toUInt8
, this function converts an input value to a value of type UInt8 but returns 0
in case of an error.
Syntax
toUInt8OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of ordinary Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt8OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of UInt8, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 8-bit unsigned integer value if successful, otherwise
0
. UInt8.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toUInt8OrZero('-8'),
toUInt8OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toUInt8OrZero('-8'): 0
toUInt8OrZero('abc'): 0
See also
toUInt8OrNull
Like toUInt8
, this function converts an input value to a value of type UInt8 but returns NULL
in case of an error.
Syntax
toUInt8OrNull(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt8OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of UInt8, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toUInt8OrNull('8'),
toUInt8OrNull('abc')
FORMAT Vertical;
Result:
Row 1:
──────
toUInt8OrNull('8'): 8
toUInt8OrNull('abc'): ᴺᵁᴸᴸ
See also
toUInt8OrDefault
Like toUInt8
, this function converts an input value to a value of type UInt8 but returns the default value in case of an error.
If no default
value is passed then 0
is returned in case of an error.
Syntax
toUInt8OrDefault(expr[, default])
Arguments
expr
— Expression returning a number or a string representation of a number. Expression / String.default
(optional) — The default value to return if parsing to typeUInt8
is unsuccessful. UInt8.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Arguments for which the default value is returned:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt8OrDefault('0xc0fe', CAST('0', 'UInt8'));
.
If the input value cannot be represented within the bounds of UInt8, overflow or underflow of the result occurs. This is not considered an error.
Returned value
- 8-bit unsigned integer value if successful, otherwise returns the default value if passed or
0
if not. 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
Query:
SELECT
toUInt8OrDefault('8', CAST('0', 'UInt8')),
toUInt8OrDefault('abc', CAST('0', 'UInt8'))
FORMAT Vertical;
Result:
Row 1:
──────
toUInt8OrDefault('8', CAST('0', 'UInt8')): 8
toUInt8OrDefault('abc', CAST('0', 'UInt8')): 0
See also
toUInt16
Converts an input value to a value of type UInt16
. Throws an exception in case of an error.
Syntax
toUInt16(expr)
Arguments
expr
— Expression returning a number or a string representation of a number. Expression.
Supported arguments:
- Values or string representations of type (U)Int8/16/32/64/128/256.
- Values of type Float32/64.
Unsupported arguments:
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt16('0xc0fe');
.
If the input value cannot be represented within the bounds of UInt16, overflow or underflow of the result occurs.
This is not considered an error.
For example: SELECT toUInt16(65536) == 0;
.
Returned value
- 16-bit unsigned integer value. UInt16.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toUInt16(16),
toUInt16(16.16),
toUInt16('16')
FORMAT Vertical;
Result:
Row 1:
──────
toUInt16(16): 16
toUInt16(16.16): 16
toUInt16('16'): 16
See also
toUInt16OrZero
Like toUInt16
, this function converts an input value to a value of type UInt16 but returns 0
in case of an error.
Syntax
toUInt16OrZero(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return 0
):
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt16OrZero('0xc0fe');
.
If the input value cannot be represented within the bounds of UInt16, overflow or underflow of the result occurs. This is not considered as an error.
Returned value
- 16-bit unsigned integer value if successful, otherwise
0
. UInt16.
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toUInt16OrZero('16'),
toUInt16OrZero('abc')
FORMAT Vertical;
Result:
Row 1:
─ ─────
toUInt16OrZero('16'): 16
toUInt16OrZero('abc'): 0
See also
toUInt16OrNull
Like toUInt16
, this function converts an input value to a value of type UInt16 but returns NULL
in case of an error.
Syntax
toUInt16OrNull(x)
Arguments
x
— A String representation of a number. String.
Supported arguments:
- String representations of (U)Int8/16/32/128/256.
Unsupported arguments (return \N
)
- String representations of Float32/64 values, including
NaN
andInf
. - String representations of binary and hexadecimal values, e.g.
SELECT toUInt16OrNull('0xc0fe');
.
If the input value cannot be represented within the bounds of UInt16, overflow or underflow of the result occurs. This is not considered an error.
Returned value
The function uses rounding towards zero, meaning it truncates fractional digits of numbers.
Example
Query:
SELECT
toUInt16OrNull('16'),
toUInt16OrNull('abc')
FORMAT Vertical;
Result:
Row 1: