Skip to main content

Conversion Functions

Introduced or updated: v1.2.187

This page lists functions that allow you to convert an expression from one data type to another.

FunctionDescriptionExampleResult
CAST( expr AS data_type )Converts a value from one data type to anotherCAST(1 AS VARCHAR)1
expr::data_typeAlias for CAST1::VARCHAR1
TRY_CAST( expr AS data_type )Converts a value from one data type to another. Returns NULL on error.TRY_CAST(1 AS VARCHAR)1
TO_BITMAP( expr )Converts a value to BITMAP data typeTO_BITMAP('1101')1101
BUILD_BITMAP( expr )Converts an array of positive integers to a BITMAP valueBUILD_BITMAP([1,4,5])::String1,4,5
TO_BOOLEAN( expr )Converts a value to BOOLEAN data typeTO_BOOLEAN('true')1
TO_FLOAT32( expr )Converts a value to FLOAT32 data typeTO_FLOAT32('1.2')1.2
TO_FLOAT64( expr )Converts a value to FLOAT64 data typeTO_FLOAT64('1.2')1.2
TO_INT8( expr )Converts a value to INT8 data typeTO_INT8('123')123
TO_INT16( expr )Converts a value to INT16 data typeTO_INT16('123')123
TO_INT32( expr )Converts a value to INT32 data typeTO_INT32('123')123
TO_INT64( expr )Converts a value to INT64 data typeTO_INT64('123')123
TO_STRING( expr )Converts a value to STRING data typeTO_STRING(10)10
TO_STRING( expr, expr )Alias for DATE_FORMATTO_STRING('2022-12-25', 'Month/Day/Year: %m/%d/%Y')Month/Day/Year: 12/25/2022
TO_UINT8( expr )Converts a value to UINT8 data typeTO_UINT8('123')123
TO_UINT16( expr )Converts a value to UINT16 data typeTO_UINT16('123')123
TO_UINT32( expr )Converts a value to UINT32 data typeTO_UINT32('123')123
TO_UINT64( expr )Converts a value to UINT64 data typeTO_UINT64('123')123
TO_VARIANT( expr )Converts a value to VARIANT data typeTO_VARIANT(TO_BITMAP('100,200,300'))[100,200,300]
  • When converting from floating-point, decimal numbers, or strings to integers or decimal numbers with fractional parts, Databend rounds the values to the nearest integer. This is determined by the setting numeric_cast_option (defaults to 'rounding') which controls the behavior of numeric casting operations. When numeric_cast_option is explicitly set to 'truncating', Databend will truncate the decimal part, discarding any fractional values.

    Example:
    SELECT CAST('0.6' AS DECIMAL(10, 0)), CAST(0.6 AS DECIMAL(10, 0)), CAST(1.5 AS INT);

    ┌──────────────────────────────────────────────────────────────────────────────────┐
    │ cast('0.6' as decimal(10, 0)) │ cast(0.6 as decimal(10, 0)) │ cast(1.5 as int32)
    ├───────────────────────────────┼─────────────────────────────┼────────────────────┤
    112
    └──────────────────────────────────────────────────────────────────────────────────┘

    SET numeric_cast_option = 'truncating';

    SELECT CAST('0.6' AS DECIMAL(10, 0)), CAST(0.6 AS DECIMAL(10, 0)), CAST(1.5 AS INT);

    ┌──────────────────────────────────────────────────────────────────────────────────┐
    │ cast('0.6' as decimal(10, 0)) │ cast(0.6 as decimal(10, 0)) │ cast(1.5 as int32)
    ├───────────────────────────────┼─────────────────────────────┼────────────────────┤
    001
    └──────────────────────────────────────────────────────────────────────────────────┘

    The table below presents a summary of numeric casting operations, highlighting the casting possibilities between different source and target numeric data types. Please note that, it specifies the requirement for String to Integer casting, where the source string must contain an integer value.

    Source TypeTarget Type
    StringDecimal
    FloatDecimal
    DecimalDecimal
    FloatInt
    DecimalInt
    String (Int)Int
  • Databend also offers a variety of functions for converting expressions into different date and time formats. For more information, see Date & Time Functions.