Data type conversion rules

2023-12-25 08:49:42  Updated

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 specify 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 INSERT and UPDATE statements, OceanBase Database converts a variable value to the data type of the affected column.

  • When executing a SELECT FROM statement, OceanBase Database converts data of the affected column to 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 underlying 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 CLOB value is converted into a character type such as VARCHAR2, or a BLOB value is converted into RAW data, OceanBase Database returns an error if the size of the data to be converted is larger than the target data type.

  • When you convert a timestamp value into a DATE value, the fractional seconds of the timestamp value are truncated and rounded.

  • Conversions from BINARY_FLOAT values to BINARY_DOUBLE values are exact.

  • Conversions from BINARY_DOUBLE to BINARY_FLOAT are inexact if the BINARY_DOUBLE value uses more bits of precision than supported by the BINARY_FLOAT data type.

  • During the comparison between character values and DATE values, OceanBase Database converts character values into DATE values.

  • During value assignment, OceanBase Database converts the value on the right side of the equal sign (=) to the data type of the target of the assignment on the left side.

  • During concatenation operations, OceanBase Database converts from noncharacter data types to 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 to a VARCHAR2 string, and then convert the obtained VARCHAR2 string to 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;
    ORA-01722: invalid number
    
  • Implicitly convert the '2' string from the CHAR data type to 2 of the NUMBER data 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 to 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

Contact Us