Explicit cast

An explicit cast governs what data conversion an end user can specify for UDTs (such as opaque data types, distinct data types, and row types). The database server invokes an explicit cast only when it encounters one of the following syntax structures:
  • The CAST AS keywords
    For example, the following expression uses the CAST AS keywords to invoke an explicit cast between the percent and INTEGER data types:
    WHERE col1 > (CAST percent AS INTEGER)
  • The :: cast operator
    For example, the following expression uses the cast operator to invoke an explicit cast between the percent and INTEGER data types:
    WHERE col1 > (percent::INTEGER)

The conversion of one data type to another can involve loss of data. If you define such conversions as explicit casts, the end user can control when the loss of data that is inherent to such a conversion is acceptable.

To create an explicit cast, specify the EXPLICIT keyword of the CREATE CAST statement. If you omit the keyword, the default is an explicit cast. Both of the following CREATE CAST statements create explicit casts from the percent data type to the INTEGER data type:
CREATE EXPLICIT CAST (percent AS INTEGER)
CREATE CAST (percent AS INTEGER)