Cast row types

You can compare or substitute between values of any two row types (named or unnamed) only when both row types have the same number of fields, and one of the following conditions is also true:
  • All corresponding fields of the two row types have the same data type.

    Two row types are considered structurally equivalent when they have the same number of fields and the data types of corresponding fields are the same.

  • User-defined casts exist to perform the conversions when two named row types are being compared.
  • System-defined or user-defined casts exist to perform the necessary conversions for corresponding field values that are not of the same data type.

    When the corresponding fields are not of the same data type, you can use either system-defined casts or user-defined casts to handle data conversions on the fields.

If a built-in cast exists to handle data conversions on the individual fields, you can explicitly cast the value of one row type to the other row type (unless the row types are both unnamed row types, in which case an explicit cast is not necessary).

If a built-in cast does not exist to handle field conversions, you can create a user-defined cast to handle the field conversions. The cast can be either implicit or explicit.

In general, when a row type is cast to another row type, the individual field conversions might be handled with explicit or implicit casts. When the conversion between corresponding fields requires an explicit cast, the value of the field that is cast must match the value of the corresponding field exactly, because the database server applies no additional implicit casts on a value that has been explicitly cast.