Skip to main content

Conversion Functions

This page provides a comprehensive overview of Conversion functions in Databend, organized by functionality for easy reference.

Type Conversion Functions

FunctionDescriptionExample
CASTConverts a value to a specified data typeCAST('123' AS INT)123
TRY_CASTSafely converts a value to a specified data type, returning NULL on failureTRY_CAST('abc' AS INT)NULL
TO_BOOLEANConverts a value to BOOLEAN typeTO_BOOLEAN('true')true
TO_STRINGConverts a value to STRING typeTO_STRING(123)'123'
TO_VARCHARConverts a value to VARCHAR typeTO_VARCHAR(123)'123'
TO_TEXTConverts a value to TEXT typeTO_TEXT(123)'123'

Numeric Conversion Functions

FunctionDescriptionExample
TO_INT8Converts a value to INT8 typeTO_INT8('123')123
TO_INT16Converts a value to INT16 typeTO_INT16('123')123
TO_INT32Converts a value to INT32 typeTO_INT32('123')123
TO_INT64Converts a value to INT64 typeTO_INT64('123')123
TO_UINT8Converts a value to UINT8 typeTO_UINT8('123')123
TO_UINT16Converts a value to UINT16 typeTO_UINT16('123')123
TO_UINT32Converts a value to UINT32 typeTO_UINT32('123')123
TO_UINT64Converts a value to UINT64 typeTO_UINT64('123')123
TO_FLOAT32Converts a value to FLOAT32 typeTO_FLOAT32('123.45')123.45
TO_FLOAT64Converts a value to FLOAT64 typeTO_FLOAT64('123.45')123.45

Binary and Specialized Conversion Functions

FunctionDescriptionExample
TO_BINARYConverts a value to BINARY typeTO_BINARY('abc')binary value
TRY_TO_BINARYSafely converts a value to BINARY type, returning NULL on failureTRY_TO_BINARY('abc')binary value
TO_HEXConverts a value to hexadecimal stringTO_HEX(255)'FF'
TO_VARIANTConverts a value to VARIANT typeTO_VARIANT('{"a": 1}'){"a": 1}
BUILD_BITMAPBuilds a bitmap from an array of integersBUILD_BITMAP([1,2,3])bitmap value
TO_BITMAPConverts a value to BITMAP typeTO_BITMAP([1,2,3])bitmap value

Please note the following when converting a value from one type to another:

  • 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.

Explore Databend Cloud for FREE
Low-cost
Fast Analytics
Easy Data Ingestion
Elastic Scaling
Try it today