Generally, an expression cannot contain values of different data types. To facilitate the calculation of expressions, OceanBase Database supports implicit and explicit conversion of values from one data type to another. This topic describes rules for implicit and explicit conversion of data types.
Notice
We recommend that you use explicit conversion, instead of implicit or automatic conversion.
Implicit data type conversion
OceanBase Database automatically converts a value from one data type to another when the conversion makes sense.
Implicit data type conversions adhere to the following rules:
When executing
INSERTandUPDATEstatements, OceanBase Database converts a variable value into the data type of the affected column.When executing a
SELECT FROMstatement, OceanBase Database converts data of the affected column into the type of the target variable.During the comparison between a character value and a numeric value, OceanBase Database converts the character value into a numeric value.
When processing numeric values, OceanBase Database adjusts the precision and scale. As a result, the numeric data type resulting from the adjustment can be different from the numeric data type in the base tables.
Conversions between character or NUMBER values and floating-point number values may not be exact, because the character types and the NUMBER type use decimal precision to represent the value, whereas floating-point numbers use binary precision.
When a
CLOBvalue is converted into a character type such asVARCHAR2, or aBLOBvalue is converted intoRAWdata, OceanBase Database returns an error if the length of the data to be converted exceeds the maximum length supported by the target data type.When you convert a timestamp value into a
DATEvalue, the fractional seconds of the timestamp value are truncated and rounded.Conversions from
BINARY_FLOATvalues toBINARY_DOUBLEvalues are exact.Conversions from
BINARY_DOUBLEtoBINARY_FLOATare inexact if theBINARY_DOUBLEvalue uses more bits of precision than supported by theBINARY_FLOATdata type.During the comparison between character values and
DATEvalues, OceanBase Database converts character values intoDATEvalues.During value assignment, OceanBase Database converts the value on the right side of the equal sign (=) into the data type of the target of the assignment on the left side.
During concatenation operations, OceanBase Database converts noncharacter data types into a character type or a national character type.
Implicit data type conversion matrix
The following table lists a matrix of implicit conversions of all data types, without considering of the conversion direction or context.
Note
"-" indicates that the conversion is not supported.
| Data type | CHAR | VARCHAR2 | NCHAR | NVARCHAR2 | DATE | DATETIME/INTERVAL | NUMBER | BINARY_FLOAT | BINARY_DOUBLE | RAW | CLOB | BLOB |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CHAR | - | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| VARCHAR2 | Yes | - | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | - |
| NCHAR | Yes | Yes | - | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | - |
| NVARCHAR2 | Yes | Yes | Yes | - | Yes | Yes | Yes | Yes | Yes | Yes | Yes | - |
| DATE | Yes | Yes | Yes | Yes | - | - | - | - | - | - | - | - |
| DATETIME / INTERVAL | Yes | Yes | Yes | Yes | - | - | - | - | - | - | - | - |
| NUMBER | Yes | Yes | Yes | Yes | - | - | - | Yes | Yes | - | - | - |
| BINARY_FLOAT | Yes | Yes | Yes | Yes | - | - | Yes | - | Yes | - | - | - |
| BINARY_DOUBLE | Yes | Yes | Yes | Yes | - | - | Yes | Yes | - | - | - | - |
| RAW | Yes | Yes | Yes | Yes | - | Yes 1 | - | - | - | - | Yes | - |
| CLOB | Yes | Yes | Yes | Yes | - | - | - | - | - | - | - | Yes |
| BLOB | - | - | - | - | - | - | - | - | - | Yes | - | - |
1 You cannot directly convert RAW data to an INTERVAL value, but you can use UTL_RAW.CAST_TO_VARCHAR2([RAW]) to convert RAW data into a VARCHAR2 string, and then convert the obtained VARCHAR2 string into an INTERVAL value.
Implicit conversion between different character types
The following table lists the directions of implicit conversion between different character types.
Note
"-" indicates that the conversion is not supported.
| Data type | To CHAR | To VARCHAR2 | To NCHAR | To NVARCHAR2 |
|---|---|---|---|---|
| From CHAR | - | VARCHAR2 | NCHAR | NVARCHAR2 |
| From VARCHAR2 | VARCHAR2 | - | NVARCHAR2 | NVARCHAR2 |
| From NCHAR | NCHAR | NCHAR | - | NCHAR2 |
| From NVARCHAR2 | NVARCHAR2 | NVARCHAR2 | NVARCHAR2 | - |
Examples of implicit data type conversion
When you execute the following statement, OceanBase Database uses implicit data type conversion to convert
'james'into a numeric value. However, the conversion fails, and an error is returned.obclient> SELECT 5 * 10 + 'james' FROM DUAL; OBE-01722: invalid numberImplicitly convert the
'2'string from theCHARdata type to 2 of theNUMBERdata type. The calculation result is 52.obclient> SELECT 5 * 10 + '2' FROM DUAL; +----------+ | 5*10+'2' | +----------+ | 52 | +----------+ 1 row in set
Explicit data type conversion
You can use SQL conversion functions to convert data of different types. The SQL functions explicitly convert data of one type into another.
Explicit data type conversion matrix
| Data type | To CHAR, VARCHAR2, NCHAR, NVARCHAR2 | To NUMBER | To DATETIME/INTERVAL | To RAW | To CLOB, BLOB | To BINARY_FLOAT | To BINARY_DOUBLE |
|---|---|---|---|---|---|---|---|
| From CHAR, VARCHAR2, NCHAR, NVARCHAR2 | TO_CHAR(char . ), TO_NCHAR(char . ) | TO_NUMBER | TO_DATE, TO_TIMESTAMP, TO_TIMESTAMP_TZ, TO_YMINTERVAL, TO_DSINTERVAL | HEXTORAW | TO_CLOB | TO_BINARY_FLOAT | TO_BINARY_DOUBLE |
| From NUMBER | TO_CHAR(number), TO_NCHAR(number) | - | TO_DATE, NUMTOYM_INTERVAL, NUMTOOLS_INTERVAL | - | - | TO_BINARY_FLOAT | TO_BINARY_DOUBLE |
| From Datetime/Interval | TO_CHAR(date ), TO_NCHAR(date) | - | - | - | - | - | - |
| From RAW | RAWTOHEX, RAWTONHEX | - | - | - | TO_BLOB | - | - |
| From CLOB、BLOB | TO_CHAR, TO_NCHAR | - | - | - | TO_CLOB | - | - |
| From BINARY_FLOAT | TO_CHAR(char . ), TO_NCHAR(char . ) | TO_NUMBER | - | - | - | TO_BINARY_FLOAT | TO_BINARY_DOUBLE |
| From BINARY_DOUBLE | TO_CHAR(char . ), TO_NCHAR(char . ) | TO_NUMBER | - | - | - | TO_BINARY_FLOAT | TO_BINARY_DOUBLE |
Examples of explicit data type conversion
Use the TO_CHAR function to explicitly convert the current time to the specified format.
obclient> SELECT TO_CHAR(SYSDATE, 'YYYY_MM_DD') FROM DUAL;
+-------------------------------+
| TO_CHAR(SYSDATE,'YYYY_MM_DD') |
+-------------------------------+
| 2021_11_30 |
+-------------------------------+
1 row in set