This document introduces the basic syntax and examples of type conversion functions.
If you need to distinguish more detailed data types when querying and analyzing data, you can use type conversion functions for data type conversion in query and analysis statements.
Function | Syntax | Description |
---|---|---|
cast | cast(x as type) | Parses the data type of x .During cast execution, if a value fails to be parsed, the system terminates the entire query and analysis operation. |
try_cast | try_cast(x as type) | Parses the data type of x .During try_cast execution, if a value fails to be parsed, the system returns NULL and continues processing by skipping the value. |
typeof | typeof(x) | Returns the data type of x . |
Note:When dirty data may exist in logs, you are advised to use the
try_cast
function to avoid query and analysis failures caused by dirty data.
The cast
function is used to parse the data type of x
. During cast
execution, if a value fails to be parsed, the system terminates the entire query and analysis operation.
cast(x as type)
Parameter | Description |
---|---|
x | The parameter value can be of any type. |
type | SQL data type. Valid values: bigint , varchar , double , boolean , timestamp , decimal , array , or map .For the mappings between index and SQL data types, please see Appendix: Data Type Mappings. |
If type
is timestamp
, x
must be a timestamp in milliseconds (such as 1597807109000) or a time string in the ISO 8601 time format (such as 2019-12-25T16:17:01+08:00).
The return value type is determined by the type
parameter.
* | select cast(0.01 as bigint)
__TIMESTAMP__
to TIMESTAMP
.* | select cast(__TIMESTAMP__ as timestamp)
The try_cast
function is used to parse the data type of x
. During try_cast
execution, if a value fails to be parsed, the system returns NULL
and continues processing by skipping the value.
try_cast(x as type)
Parameter | Description |
---|---|
x | The parameter value can be of any type. |
type | SQL data type. Valid values: bigint , varchar , double , boolean , timestamp , decimal , array , or map .For the mappings between index and SQL data types, please see Appendix: Data Type Mappings. |
The return value type is determined by the type
parameter.
Parse the remote_user field value into a VARCHAR value:
* | select try_cast(remote_user as varchar)
The mappings between index and SQL data types are as follows:
Index Data Type | SQL Data Type |
---|---|
long | bigint |
text | varchar |
double | double |
json | varchar |
Was this page helpful?