Creating a cast that has a cast function

About this task

You can create special SQL-invoked functions, called cast functions, that implement data conversion between two dissimilar data types. When two data types have different storage structures, you must create a cast function that defines how to convert the data in the source data type to data of the target data type.

To create a cast that has a cast function:

Procedure

  1. Write the cast function.

    The cast function takes the source data type as its argument and returns the target data type.

  2. Register the cast function with the CREATE FUNCTION statement.
  3. Register the cast with the CREATE CAST statement.

    Use the WITH clause of the CREATE CAST statement to specify the cast function. To invoke a cast function, the function must reside in the current database. However, the cast function does not need to exist when you register the cast.