Use type conversion functions in query and analysis statements to convert data types.
SLS supports the following type conversion functions.
If you want to use strings in analytic statements, you must enclose the strings in single quotation marks (''). Strings that are not enclosed or strings that are enclosed in double quotation marks ("") indicate field names or column names. For example, 'status' indicates the status string, and status or "status" indicates the status log field.
|
Function |
Syntax |
Description |
SQL |
SPL |
|
cast(x as type) |
Converts the data type of x. If conversion fails, the entire query fails. |
√ |
√ |
|
|
try_cast(x as type) |
Converts the data type of x. If conversion fails, returns NULL and continues processing. Note
Use try_cast instead of cast to prevent dirty data from stopping the entire query. |
√ |
× |
|
|
typeof(x) |
Returns the data type of x. |
√ |
× |
Cast function
Converts the data type of x. If conversion fails, the entire query fails.
Syntax
cast(x as type)
Parameters
|
Parameter |
Description |
|
x |
The expression to convert. Supports any data type. |
|
type |
The target SQL data type. Valid values: Example: Appendix: Data type mapping lists the index-to-SQL type mappings. |
Return value type
The data type specified by the type parameter.
Examples
SQL
Convert 1 to boolean.
-
Query and analysis statement
* | select cast(1 as boolean) -
The query returns
true.
SPL
Convert 1 to boolean.
-
Query and analysis statement
* | extend a = cast(1 as boolean)
-
The value of a is
true.
Try_cast function
Converts the data type of x. If conversion fails, returns NULL and continues processing.
Syntax
try_cast(x as type)
Parameters
|
Parameter |
Description |
|
x |
The expression to convert. Supports any data type. |
|
type |
The target SQL data type. Valid values: Example: Appendix: Data type mapping lists the index-to-SQL type mappings. |
Return value type
The data type specified by the type parameter.
Examples
Convert the uid field to varchar.
-
Query and analysis statement
* | select try_cast(uid as varchar) -
The result contains a
_col0column. Both rows have the valueowner-01.
Typeof function
Returns the data type of x.
Syntax
typeof(x)
Parameters
|
Parameter |
Description |
|
x |
The expression to evaluate. Supports any data type. |
Return value type
varchar
Examples
Check the data type of request_time.
-
Query and analysis statement
* |SELECT typeof(request_time) -
The data type of
request_timeisdouble.
Appendix: Data type mapping
Index-to-SQL data type mappings:
|
Index data type |
SQL data type |
|
long |
bigint |
|
text |
varchar |
|
double |
double |
|
json |
varchar |