Skip to content

TREAT

  • object: This is the expression that you want to convert from its original data type to the target data type specified. Object can be a subquery, column, a function that returns a value, or a value in the valid data type. It works with subtypes of user-defined types including object types and collection types.
  • as type: This represents the target data type to which you want to convert the object. Type can be an SQL standard data type or a user-defined type including object types and collection types. The database will attempt to convert the object to the specified type and if the conversion is not possible, an error will be raised.
SELECT TREAT(REF(er) AS SPECIALIZED_EMP_T) AS emp
FROM EMP_RECTABLE er
WHERE VALUE(er) IS OF (ONLY SPECIALIZED_EMP_T);
EMP
---
SPECIALIZED_EMP_T('Alex', 'Marketing', 1500, 30)
SPECIALIZED_EMP_T('Melinda', 'HR', 2000, 20)

The TREAT function is used in Oracle to treat a substitutable type or a substitutable column as a specific subtype. In the given example, it treats each row of the EMP_RECTABLE as the subtype SPECIALIZED_EMP_T and returns the row only if the row is precisely of the specified subtype.