跳到主要内容
版本: 最新版本-3.5

CAST

将输入转换为指定的类型。例如,cast (input as BIGINT) 将输入转换为 BIGINT 值。

从 v2.4 版本起,StarRocks 支持转换为 ARRAY 类型。

语法

cast (input as type)

参数

input:要转换的数据。type:目标数据类型。

返回值

返回一个数据类型与 type 相同的值。

示例

示例 1:常见数据转换

    select cast('9.5' as DECIMAL(10,2));
+--------------------------------+
| CAST('9.5' AS DECIMAL64(10,2)) |
+--------------------------------+
| 9.50 |
+--------------------------------+

select cast(NULL AS INT);
+-------------------+
| CAST(NULL AS INT) |
+-------------------+
| NULL |
+-------------------+

select cast(true AS BOOLEAN);
+-----------------------+
| CAST(TRUE AS BOOLEAN) |
+-----------------------+
| 1 |
+-----------------------+

select cast (1 as BIGINT);
+-------------------+
| CAST(1 AS BIGINT) |
+-------------------+
| 1 |
+-------------------+

示例 2:将输入转换为 ARRAY。

    -- Convert string to ARRAY<ANY>.

select cast('[1,2,3]' as array<int>);
+-------------------------------+
| CAST('[1,2,3]' AS ARRAY<INT>) |
+-------------------------------+
| [1,2,3] |
+-------------------------------+

select cast('[1,2,3]' as array<bigint>);
+----------------------------------+
| CAST('[1,2,3]' AS ARRAY<BIGINT>) |
+----------------------------------+
| [1,2,3] |
+----------------------------------+

select cast('[1,2,3]' as array<string>);
+------------------------------------------+
| CAST('[1,2,3]' AS ARRAY<VARCHAR(65533)>) |
+------------------------------------------+
| ["1","2","3"] |
+------------------------------------------+

select cast('["a", "b", "c"]' as array<string>);
+--------------------------------------------------+
| CAST('["a", "b", "c"]' AS ARRAY<VARCHAR(65533)>) |
+--------------------------------------------------+
| ["a","b","c"] |
+--------------------------------------------------+

-- Convert JSON array to ARRAY<ANY>.

select cast(parse_json('[{"a":1}, {"a": 2}]') as array<json>);
+----------------------------------------------------------+
| CAST((parse_json('[{"a":1}, {"a": 2}]')) AS ARRAY<JSON>) |
+----------------------------------------------------------+
| ['{"a": 1}','{"a": 2}'] |
+----------------------------------------------------------+

select cast(parse_json('[1, 2, 3]') as array<int>);
+-----------------------------------------------+
| CAST((parse_json('[1, 2, 3]')) AS ARRAY<INT>) |
+-----------------------------------------------+
| [1,2,3] |
+-----------------------------------------------+

select cast(parse_json('["1","2","3"]') as array<string>);
+--------------------------------------------------------------+
| CAST((parse_json('["1","2","3"]')) AS ARRAY<VARCHAR(65533)>) |
+--------------------------------------------------------------+
| ["1","2","3"] |
+--------------------------------------------------------------+

示例 3:加载期间转换数据。

    curl --location-trusted -u <username>:<password> -T ~/user_data/bigint \
-H "columns: tmp_k1, k1=cast(tmp_k1 as BIGINT)" \
http://host:port/api/test/bigint/_stream_load

注意

如果原始值是浮点值(例如 12.0),它将被转换为 NULL。 如果您想强制将此类型转换为 BIGINT,请参见以下示例

    curl --location-trusted -u <username>:<password> -T ~/user_data/bigint \
-H "columns: tmp_k1, k1=cast(cast(tmp_k1 as DOUBLE) as BIGINT)" \
http://host:port/api/test/bigint/_stream_load
    MySQL > select cast(cast ("11.2" as double) as bigint);
+----------------------------------------+
| CAST(CAST('11.2' AS DOUBLE) AS BIGINT) |
+----------------------------------------+
| 11 |
+----------------------------------------+