INFORMIX
Informix Guide to SQL: Tutorial
Chapter 13: Casting Data Types
Home Contents Index Master Index New Book

What Is a Cast?

A cast is a mechanism that converts a value from one data type to another data type. Casts allow you to make comparisons between values of different data types or substitute a value of one data type for a value of another data type. Casts are supported in the following types of expressions:

  • Column expressions
  • Constant expressions
  • Function expressions
  • SPL variables
  • Host variables (ESQL)
  • Statement local variable (SLV) expressions
To convert a value of one data type to another data type, a cast must exist in the database or in the database server. Universal Server supports three kinds of cast:

  • System-defined casts
    A system-defined cast is a cast that is built in to the database server. A system-defined casts performs automatic conversions between different built-in data types.

  • User-defined Casts
    • Implicit cast
    A cast is implicit if you specify the implicit keyword when you create the cast. An implicit cast is invoked automatically to perform conversions between two data types.

    • Explicit cast
    A cast is explicit if you specify the explicit keyword when you create the cast. (The default is explicit.) To invoke an explicit cast, you must use the CAST AS keywords or the double colon (::) cast operator. Explicit casts are never invoked automatically.

For information about system-defined casts, see Chapter 2 of the Informix Guide to SQL: Reference.

For the syntax that you use to create a user-defined cast, see the CREATE CAST statement in the Informix Guide to SQL: Syntax.

Creating User-Defined Casts

To perform conversions between two data types when no cast exists to convert values of one data type to the other, you can create a user-defined cast. A user-defined cast is a cast you create with the CREATE CAST statement. User-defined casts are typically used to provide data type conversions for the following extended data types:

  • Opaque data types. Developers of opaque data types must define casts to handle conversions between the internal/external representations of the opaque data type. For information about how to create and register casts for opaque data types, see the Extending INFORMIX-Universal Server: Data Types manual.
  • Distinct data types. You cannot directly compare a distinct data type to its source type. However, Universal Server automatically registers explicit casts from the distinct type to the source type and vice versa. Although a distinct type inherits the casts that are defined on its source type, any user-defined casts that you define on a distinct type are not available to its source type. For more information and examples that show how you can create and use casts for distinct types, see "An Example of Casts with Conversion Functions".
  • Named row types. In most cases, you can explicitly cast a named row type to another row-type value without having to create the cast. However, in some cases, you might want to create a cast to convert between a named row type and some other data type.
Important: You cannot create more than one cast to handle conversions between the same two data types.
For information about how to create user-defined casts, see the Extending INFORMIX-Universal Server: Data Types manual.

Invoking Casts

For system-defined and implicit user-defined casts, the database server automatically (implicitly) invokes the cast to handle the data conversion. For example, you can compare a value of type INT with SMALLINT, FLOAT, or CHAR values without explicitly casting the expression because system-defined casts automatically handle the conversions between these built-in data types.

When an explicit cast has been defined to handle conversions between two data types, you must explicitly invoke the cast with the CAST... AS keywords or the double-colon cast operator (::). The following partial examples show the two ways that you can invoke an explicit cast:

In general, a cast between two data types assumes that each data type represents an equal number of component values. For example, a cast between a row type and an opaque data type is possible if each field in the row type has a corresponding field in the opaque data type. Of course, to perform a cast between an opaque data type and a named row type, you would first need to create the conversion function and register it as a cast with the CREATE CAST statement.

You cannot create a user-defined cast that includes any of the following data types as either the source type or target type for the cast:

  • Collection data types
  • Unnamed row types
  • BLOB
  • CLOB
  • TEXT
  • BYTE



Informix Guide to SQL: Tutorial, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.