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 decimal to integer
select cast(2.0 AS integer) from dual;

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

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

IN THIS PAGE