informix
Informix Guide to SQL: Reference
Data Types

Data Type Casting and Conversion

Occasionally, the data type that was assigned to a column with the CREATE TABLE statement is inappropriate. You might want to change the data type of a column when you need to store larger values than the current data type can accommodate. The database server allows you to change the data type of the column or to cast its values to a different data type with either of the following methods:

If you change data types, the new data type must be able to store all the old values. For example, if you try to convert a column from the INTEGER data type to the SMALLINT data type and the following values exist in the INTEGER column, the database server does not change the data type because SMALLINT columns cannot accommodate numbers greater than 32,768:

The same situation might occur if you attempt to transfer data from FLOAT or SMALLFLOAT columns to INTEGER, SMALLINT, or DECIMAL columns.

Both data type conversion and casting depend on casts defined in the syscasts system catalog table. For information about syscasts, see SYSCASTS.

A cast is either built-in or user-defined. Guidelines exist for casting distinct and extended data types.

For more information about casting opaque types, see Extending Informix Dynamic Server 2000. For information about casting other extended types, see the Informix Guide to Database Design and Implementation.

Using Built-in Casts

User informix owns built-in casts. They govern conversions from one built-in data type to another. Built-in casts allow the database server to convert:

The database server automatically invokes the appropriate built-in casts when required. An infinite number of built-in casts might be invoked to evaluate and compare expressions or to change a column from one built-in data type to another.

When you convert a column from one built-in data type to another, the database server applies the appropriate built-in casts to each value already in the column. If the new data type cannot store any of the resulting values, the ALTER TABLE statement fails.

For example, if you try to convert a column from the INTEGER data type to the SMALLINT data type and the following values exist in the INTEGER column, the database server does not change the data type because SMALLINT columns cannot accommodate numbers greater than 32,767:

The same situation might occur if you attempt to transfer data from FLOAT or SMALLFLOAT columns to INTEGER, SMALLINT, or DECIMAL columns.

The following sections describe database server behavior during certain types of casts and conversions.

Converting from Number to Number

When you convert data from one number data type to another, you occasionally find rounding errors. Figure 2-14 on page 2-65 indicates which numeric data type conversions are acceptable and what kinds of errors you can encounter when you convert between certain numeric data types.

Figure 2-14
Numeric Data Type Conversion Chart

TO
FROM SMALLINT INTEGER INT8 SMALLFLOAT FLOAT DECIMAL
SMALLINT OK OK OK OK OK OK
INTEGER E OK OK E OK P
INT8 E E OK D E P
SMALLFLOAT E E E OK OK P
FLOAT E E E D OK P
DECIMAL E E E D D P
Legend:
OK = No error
P = An error can occur depending on the precision of the decimal
E = An error can occur depending on data
D = No error, but less significant digits might be lost

For example, if you convert a FLOAT value to DECIMAL(4,2), your database server rounds off the floating-point numbers before storing them as decimal numbers. This conversion can result in an error depending on the precision assigned to the DECIMAL column.

Converting Between Number and CHAR

You can convert a CHAR (or NCHAR) column to a numeric column. However, if the CHAR or NCHAR column contains any characters that are not valid in a number column (for example, the letter l instead of the number 1), your database server returns an error.

You can also convert a numeric column to a character column. However, if the character column is not large enough to receive the number, the database server generates an error.

If the database server generates an error, it cannot complete the ALTER TABLE statement or cast and leaves the column values as characters. You receive an error message and the statement is rolled back (whether you are in a transaction or not).

Converting Between INTEGER and DATE or DATETIME

You can convert an integer column (SMALLINT, INTEGER, or INT8) to a DATE or DATETIME value. The database server interprets the integer as a value in the internal format of the DATE or DATETIME column. You can also convert a DATE or DATETIME column to an integer column. The database server stores the internal format of the DATE or DATETIME column as an integer.

For a DATE column, the internal format is a Julian date. For a DATETIME column, the internal format stores the date and time in a condensed integer format.

Converting Between DATE and DATETIME

You can convert DATE columns to DATETIME columns. However, if the DATETIME column contains more fields than the DATE column, the database server either ignores the fields or fills them with zeros. The illustrations in the following list show how these two data types are converted (assuming that the default date format is mm/dd/yyyy):

Using User-Defined Casts

Implicit and explicit casts are owned by the users who create them. They govern casts and conversions between user-defined data types and other data types.

Developers of user-defined data types must create certain implicit and explicit casts and the functions that are used to implement them. The casts allow user-defined types to be expressed in a form that clients can manipulate.

For information on how to create and use implicit and explicit casts, see the CREATE CAST statement in the Informix Guide to SQL: Syntax and the Informix Guide to Database Design and Implementation.

Implicit Casts

The database server automatically invokes a single implicit cast when needed to evaluate and compare expressions or pass arguments. Operations that require more than one implicit cast fail.

Implicit casts allow you to convert a user-defined data type to a built-in type or vice versa.

Users can explicitly invoke an implicit cast using the CAST AS keywords or the double colon (::) cast operator.

Explicit Casts

Explicit casts, unlike implicit casts or built-in casts, are never invoked automatically by the database server. Users must invoke them explicitly with the CAST AS keywords or the double colon (::) cast operator.

Explicit casts do not allow you to convert a user-defined data type to a built-in data type or vice versa.

Determining Which Cast to Apply

The database server uses the following rules to determine which cast to apply in a particular situation:

For information about casting and the IMPEX, IMPEXBIN, LVARCHAR, and SENDRECV data types, see Extending Informix Dynamic Server 2000.

Casts for Distinct Types

You define a distinct type based on a built-in type or an existing opaque type or row type. Although data of the distinct type has the same length and alignment and is passed in the same way as data of the source type, the two cannot be compared directly. To compare a distinct type and its source type, you must explicitly cast one type to the other.

When you create a new distinct type, the database server automatically registers two explicit casts:

You can create an implicit cast between a distinct type and its source type. However, to create an implicit cast, you must first drop the default explicit cast between the distinct type and its source type.

You also can use all casts that have been registered for the source type without modification on the distinct type. You can also define new casts and support functions that apply only to the distinct type.

For examples that show how to create a cast function for a distinct type and register the function as cast, see the Informix Guide to Database Design and Implementation.

What Extended Data Types Can Be Cast?

The following table shows the data type combinations that you can cast. The table shows only whether or not a cast between a source type and a target type are possible. In some cases, you must first create a user-defined cast before you can perform a conversion between two data types. In other cases, the database server automatically provides a cast that is implicitly invoked or that you must explicitly invoke.

Target Type ---> Opaque Type Distinct Type Named Row Type Unnamed Row Type Collection Type Built-in Type
Opaque Type explicit or implicit explicit explicit3 Not Allowed Not Allowed explicit or implicit3
Distinct Type explicit explicit explicit Not Allowed Not Allowed explicit or implicit
Named Row Type explicit3 explicit explicit3 explicit1 Not Allowed Not Allowed
Unnamed Row Type Not Allowed Not Allowed explicit1 implicit1 Not Allowed Not Allowed
Collection Type Not Allowed Not Allowed Not Allowed Not Allowed explicit2 Not Allowed
Built-in Type explicit or implicit3 explicit or implicit Not Allowed Not Allowed Not Allowed system defined (implicit)
1 Applies when two row types are structurally equivalent or casts exist to handle data conversions where corresponding field types are not the same. 2 Applies when a cast exists to convert between the element types of the respective collection types. 3 Applies when a user-defined cast exists to convert between the two data types.


Informix Guide to SQL: Reference, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved