Purpose
You can call this function to convert one built-in data type to another built-in data type.
Syntax
CAST (expr AS type_name )
Parameters
| Parameter | Description |
|---|---|
| expr | The column name or expression. |
| AS | Used to separate two parameters. The data to be converted is located before AS, and the data type to which the data is to be converted is located after AS. |
| type_name | The name of a built-in data type of OceanBase Database. |
Return type
This function returns the same data type as what you specified for the type_name parameter.
The following table describes the conversion relationships among the built-in data types.
| 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 |
Note
DATETIME/INTERVALdata types includeDATE,TIMESTAMP,TIMESTAMP WITH TIMEZONE,INTERVAL DAY TO SECOND, andINTERVAL YEAR TO MONTH.
Examples
The following example converts the character string 123 to an integer, the integer 1 to the VARCHAR2(10) data type, and 22-OCT-2021 to the TIMESTAMP WITH LOCAL TIME ZONE data type.
obclient> SELECT CAST('123' AS INT),CAST(1 AS VARCHAR2(10)),CAST('22-OCT-2021' AS TIMESTAMP WITH LOCAL TIME ZONE)
AS RESULT FROM DUAL;
+------------------+-----------------------+------------------------------+
CAST('123'ASINT) CAST(1ASVARCHAR2(10)) RESULT
+------------------+-----------------------+------------------------------+
123 1 22-OCT-21 12.00.00.000000 AM
+------------------+-----------------------+------------------------------+
1 row in set
Note
This function is affected by the
NLS_DATE_FORMATparameter when you use it to convert a value of the DATE data type. You can execute the following statement to convert theNLS_DATE_FORMATformat first:ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';.