You can use type conversion functions to convert data types in a query statement.

Function Syntax Description
cast function cast(x as type) Converts the data type of x.

If you use the cast function to convert data types and a value fails to be converted, the entire query and analysis process ends.

try_cast function try_cast(x as type) Converts the data type of x.

If you use the try_cast function to convert data types and a value fails to be converted, NULL is returned for the value and the query and analysis process continues.

Note We recommend that you use the try_cast function to query and analyze logs. This prevents the failures of data queries and data analysis due to dirty data.

cast function

The cast function is used to convert the data type of x. If you use the cast function to convert data types and a value fails to be converted, the entire query and analysis process ends.

Syntax

cast(x as type)

Parameters

Parameter Description
x The value of this parameter is of an arbitrary data type.
type The SQL data type. Valid values: bigint, varchar, double, timestamp, decimal, array, and map.

For information about the mappings between index data types and SQL data types, see Data type mappings.

Return value type

The return value type is based on the type parameter that you specified.

Examples

Convert the number 1 to a value of the Boolean type.

  • Query statement
    * | select cast(1 as boolean)
  • Query and analysis resultcast

try_cast function

The try_cast function is used to convert the data type of x. If you use the try_cast function to convert data types and a value fails to be converted, NULL is returned for the value and the query and analysis process continues.

Syntax

try_cast(x as type)

Parameters

Parameter Description
x The value of this parameter is of an arbitrary data type.
type The SQL data type. Valid values: bigint, varchar, double, timestamp, decimal, array, and map.

For information about the mappings between index data types and SQL data types, see Data type mappings.

Return value type

The return value type is based on the type parameter that you specified.

Examples

Convert the value of the uid field to a value of the varchar type.

  • Query statement
    * | select try_cast(uid as varchar)
  • Query and analysis resulttry_cast

Data type mappings

The following table describes the mappings between index data types and SQL data types.

Index data type SQL data type
long bigint
text varchar
double double
json varchar