CAST
函数用于将源数据类型的表达式显式转换为另一种数据类型。
语法
CAST (expr AS type_name )
参数
参数 | 说明 |
---|---|
expr | 列名或者表达式。 |
AS | 用于分隔两个参数,在 |
type_name | 数据类型为 OceanBase 的 内建数据类型 。 |
返回类型
返回与 type_name
相同的类型。
下表显示了哪些数据类型可以转换为其他内置数据类型:
from BINARY_FLOAT, BINARY_DOUBLE | from CHAR, VARCHAR2 | from NUMBER | from DATETIME/INTERVAL | from RAW | from NCHAR, NVARCHAR2 | |
to BINARY_FLOAT, BINARY_DOUBLE | yes | yes | yes | no | no | yes |
to CHAR, VARCHAR2 | yes | yes | yes | yes | yes | no |
to NUMBER | yes | yes | yes | no | no | yes |
to DATETIME, INTERVAL | no | yes | no | yes | no | no |
to RAW | yes | yes | yes | no | yes | no |
to NCHAR, NVARCHAR2 | yes | no | yes | yes | yes | yes |
1To DATETIME/INTERVAL
数据类型包括 DATE
、TIMESTAMP
、TIMESTAMP WITH TIMEZONE
、INTERVAL DAY TO SECOND
和 INTERVAL YEAR TO MONTH
。
示例
执行以下语句:
SELECT CAST('123' AS INT),CAST(1 AS VARCHAR2(10)),CAST('22-OCT-1997' AS TIMESTAMP WITH LOCAL TIME ZONE)
AS RESULT FROM DUAL;
查询结果如下:
+------------------+-----------------------+----------------------------+
| CAST('123'ASINT) | CAST(1ASVARCHAR2(10)) | RESULT |
+------------------+-----------------------+----------------------------+
| 123 | 1 | 1997-10-22 00:00:00.000000 |
+------------------+-----------------------+----------------------------+