How to Do Type Casting

Casting operations allow us to change the data type of a given value. Not all the casts are allowed, for example, you can’t cast ABC to integer, obviously.

In Oracle you have to use the cast function which has the following syntax:

cast(value as type) 

-- Cast date to varchar
select CAST('12/22/2003' AS VARCHAR2(30)) from dual;

-- Cast date to timestamp
select cast('12/22/2003' AS TIMESTAMP) from dual;

-- Cast decimal to integer
select cast(2.0 AS INT) AS cast_result from dual;
cast_result
2

Some functions in Oracle which are similar to the CAST function are: TO_NUMBER(), TO_CHAR() and TO_DATE()

IN THIS PAGE