Data Type Conversions

In Oracle SQL, type conversion refers to the process of changing a value from one data type to another. This is often necessary for many operations, such as:

  • Performing calculations: When combining numerical values stored as different data types (e.g., NUMBER and VARCHAR2), conversion ensures compatible formats for arithmetic operations.
  • Comparing values: Comparisons between different data types (e.g., DATE and VARCHAR2) require conversion to common type for accurate results.
  • Storing values: Assigning values to columns with specific data types may necessitate conversion to match the column's definition.

Conversion functions do not change any stored values; rather, they modify the data type for the values. Consider a scenario where financial values are stored as text, including dollar signs and other symbols that a mathematical function may reject, triggering an error message. 


In such cases, a conversion function can be employed to transform the text value into a numeric data. For example: 



Implicit vs Explicit Conversion

In Oracle SQL, implicit conversion refers to the automatic data type change from one data type to another without explicit instructions from the user. It occurs automatically in circumstances when operands for an operation have different data types, Oracle tries implicit type conversion to make them compatible. For example, in the figure shown below, Oracle implicitly converted a text string ('3') into a numeric value in order to perform addition:


Oracle has a certain order of precedence for implicit type conversions. It basically prioritizes values with higher-precision and more detailed data types, meaning that the value with lower-precision will be matched to another value. Here's an overview of the hierarchy, from highest to lowest priority:

  1. Datetime and Interval Data Types:
    • Dates and timestamps, including intervals representing durations, come first in the hierarchy.
  2. BINARY_DOUBLE and BINARY_FLOAT:
    • These floating-point data types with high precision can represent a wider range of decimal values.
  3. NUMBER:
    • Numeric data type encompassing integers, decimals, and floating-point values with moderate precision.
  4. Character Data Types:
    • Includes fixed-length (CHAR, NCHAR) and variable-length (VARCHAR2, NVARCHAR2) character strings.
  5. All Other Built-in Data Types:
    • Less commonly used types like ROWID or cursors fall last in the hierarchy.

Note: Oracle's documentation cautions that the behavior of implicit conversion is subject to change across software releases. 

When applying operations on values with different data types, Oracle automatically converts the lower-priority type to align with the higher-priority one. This automatic conversion can result in unexpected outcomes if you're not aware of the hierarchy. For example, comparing a string ('10') to a number (5) will lead to the implicit conversion to a number (10) before the comparison. If this is your intension, it might be okay. If unintended, an implicit type conversion can introduce logical errors. However, it is considered good design to minimize reliance on automatic data type conversion and explicitly articulate your intentions in the code. This practice ensures that your code's intent is clearly documented and remains supported in the future.

The CAST Function

Using CAST function is one simple way to perform an explicit data type conversion. This can be useful when you need to ensure compatibility between different data types in SQL queries or when you want to display data in a specific format.

The basic syntax of the CAST function is as follows:

CAST(expression AS data_type);

Where expression is the value or column that you want to convert, and data_type is the target data type to which you want to convert the expression. For example, let's say you have a column named 'price' with data type NUMBER and you want to convert it to VARCHAR2 for display purposes. You can use the CAST function as below:


In this example, the 'price' column is cast to VARCHAR2 with a maximum length of 10 characters. You can adjust the target data type and length based on your specific requirements. 

It is important to note that the CAST function is similar to the TO_CHAR, TO_DATE, and TO_NUMBER function in Oracle SQL, but it provides a more ANSI SQL-compliant way of performing data type conversions.

Post a Comment

0 Comments