Figure 2-7 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 numeric 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 an 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):
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
Using Explicit Casts Explicit casts are owned by the users who create them. They govern casts between user-defined data types and other data types. Developers of user-defined data types must create certain 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. They do not allow you to convert a user-defined data type to a built-in type or vice versa. Explicit casts, unlike implicit casts or system-defined 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. For information on how to define explicit casts, see the CREATE CAST statement in the Informix Guide to SQL: Syntax.
Determining Which Cast to Apply The database server uses the following rules to determine which cast to apply in a particular situation:
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.
explicit or implicit
explicit
explicit3
NA
explicit or implicit3
explicit1
implicit1
explicit2
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.
NA = Not Allowed