The Oracle CAST function converts one data type to another. The CAST function can convert built-in and collection-typed values into other built-in or collection typed values.
CAST can convert a date or other unnamed operand (or a nested table or other named collection) into a type-compatible datatype or named collection. For this use of CAST, type_name and/or operand must be of (or evaulate to) a built-in datatype or collection type .
CAST and ANYDATA Type
When using CAST to convert an operand, the expr can also be either a built-in datatype or a collection type; however, it can be an instance of an ANYDATA type as well. When the expr is of an ANYDATA type, CAST attempts an extraction of the value of the ANYDATA expr and returns it if it matches the CAST target type. If the there is no match to the CAST target type, NULL is returned.
CAST with LOB Datatypes
LOB datatypes are not directly supported by the CAST function. Using CAST to convert CLOB values into a character datatypes or BLOB values into the RAW datatype results in the database converting the LOB value implicity to character or raw data. Once this implicit conversion is done, the resulting value is CAST into the target datatype. This process will throw an error if the resulting value is larger than the target type.
The Oracle docs note the syntax for Oracle CAST as follows:
CAST({ expr | MULTISET (subquery) } AS type_name)
With the Oracle CAST function a block could be re-written as:
DECLARE
v NUMBER;
BEGIN
a(CAST (v AS INTEGER));
END;
On the other hand, the function could be re-written with Oracle CAST as:
DECLARE
v NUMBER;
BEGIN
a(v::INTEGER);
END;
Each of these uses of the CAST function will produce the same result.
CAST can convert a date or other unnamed operand (or a nested table or other named collection) into a type-compatible datatype or named collection. For this use of CAST, type_name and/or operand must be of (or evaulate to) a built-in datatype or collection type .
CAST and ANYDATA Type
When using CAST to convert an operand, the expr can also be either a built-in datatype or a collection type; however, it can be an instance of an ANYDATA type as well. When the expr is of an ANYDATA type, CAST attempts an extraction of the value of the ANYDATA expr and returns it if it matches the CAST target type. If the there is no match to the CAST target type, NULL is returned.
CAST with LOB Datatypes
LOB datatypes are not directly supported by the CAST function. Using CAST to convert CLOB values into a character datatypes or BLOB values into the RAW datatype results in the database converting the LOB value implicity to character or raw data. Once this implicit conversion is done, the resulting value is CAST into the target datatype. This process will throw an error if the resulting value is larger than the target type.
The Oracle docs note the syntax for Oracle CAST as follows:
CAST({ expr | MULTISET (subquery) } AS type_name)
With the Oracle CAST function a block could be re-written as:
DECLARE
v NUMBER;
BEGIN
a(CAST (v AS INTEGER));
END;
On the other hand, the function could be re-written with Oracle CAST as:
DECLARE
v NUMBER;
BEGIN
a(v::INTEGER);
END;
Each of these uses of the CAST function will produce the same result.