informix
INFORMIX-ESQL/C Programmer's Manual
INFORMIX-ESQL/C Data Types

Data Conversion

When a discrepancy exists between the data types of two values, ESQL/C attempts to convert one of the data types. The process of converting a value from one data type to another is called data conversion.

The following list names a few common situations in which data conversion can occur:

For more information on fetching and inserting, see the next section, "Fetching and Inserting with Host Variables." For more information on arithmetic operations, see Performing Arithmetic Operations.

Fetching and Inserting with Host Variables

If you try to fetch a value from a database column into a host variable that you do not declare according to the correspondence shown in Figure 3-1 on page 3-4, ESQL/C attempts to convert the data types. Similarly, if you try to insert a value from a host variable into a database column, ESQL/C might need to convert data types if the host variable and database column do not use the correspondences in Figure 3-1. ESQL/C converts the data types only if the conversion is meaningful.

This section provides the following information on data conversion for fetching and inserting values with host variables:

Converting Numbers and Strings

Before ESQL/C can convert a value from one data type to another, it must determine whether the conversion is meaningful. Figure 3-10 shows possible conversions between numeric data types and character data types. In this figure, N represents a value with a numeric data type (such as DECIMAL, FLOAT, or SMALLINT) and C represents a value with a character data type (such as CHAR or VARCHAR).

If conversion is not possible, either because it makes no sense or because the target variable is too small to accept the converted value, ESQL/C returns values that the Results column in Figure 3-10 describes.

Figure 3-10

Conversion Problem Results
C Æ C Does not fit ESQL/C truncates the string, sets a warning (sqlca.sqlwarn.sqlwarn1 to W and SQLSTATE to 01004), and sets any indicator variable to the size of the original string. For more information, see Fetching and Inserting Character Data Types.
NÆ C None ESQL/C creates a string for the numeric value; it uses an exponential format for very large or very small numbers. For more information, see Converting Floating-Point Numbers to Strings.
NÆ C Does not fit ESQL/C fills the string with asterisks, sets a warning (sqlca.sqlwarn.sqlwarn1 to W and SQLSTATE to 01004), and sets any indicator variable to a positive integer. When the fractional part of a number does not fit in a character variable, ESQL/C rounds the number. Asterisks appear only when the integer part does not fit.
CÆ N None ESQL/C determines the numeric data type based on the format of the character value; if the character contains a decimal point, ESQL/C converts the value to a DECIMAL value.
CÆ N Not a number The number is undefined; ESQL/C sets sqlca.sqlcode and SQLSTATE to indicate a runtime error.
CÆ N Overflow The number is undefined; ESQL/C sets sqlca.sqlcode and SQLSTATE to indicate a runtime error.
N Æ N Does not fit ESQL/C attempts to convert the number to the new data type. For information on possible errors, see the Informix Guide to SQL: Reference.
N Æ N Overflow The number is undefined; ESQL/C sets sqlca.sqlcode and SQLSTATE to indicate a runtime error.
Data Conversion Problems and Results

In Figure 3-10, the phrase Does not fit means that the size of the data from the source variable or column exceeds the size of the target column or variable.

Converting Floating-Point Numbers to Strings

ESQL/C can automatically convert floating-point column values (data type of DECIMAL(n), FLOAT, or SMALLFLOAT) between database columns and host variables of character type char, varchar, string, or fixchar. When ESQL/C converts a floating-point value to a character string whose buffer is not large enough to hold the full precision, ESQL/C rounds the value to fit it in the character buffer. For more information, see Implicit Data Conversion.

Converting BOOLEAN Values to Characters

The database server can automatically convert BOOLEAN values between database columns and host variables of the fixchar date type. The following table shows the character representations for the BOOLEAN values.

BOOLEAN Value Character Representation
'\01' 'T'
'\00' 'F'

For more information on the BOOLEAN data type, see Chapter 5, Working with Numeric Data Types.

Converting DATETIME and INTERVAL Values

ESQL/C can automatically convert DATETIME and INTERVAL values between database columns and host variables of character type char, string, or fixchar. ESQL/C converts a DATETIME or INTERVAL value to a character string and then stores it in a host variable. For more information, see Implicit Data Conversion.

You can use ESQL/C library functions to explicitly convert between DATE and DATETIME values. For more information, see Data Conversion for datetime and interval Values.

Converting Between VARCHAR and Character Data Types

ESQL/C can automatically convert VARCHAR values between database columns and host variables of character type char, string, or fixchar. For more information, see Fetching and Inserting VARCHAR Data.

Performing Arithmetic Operations

When ESQL/C performs an arithmetic operation on two values, it might need to convert data types if the two values do not have data types that match. This section provides the following information on data conversion for arithmetic operations:

Converting Numbers to Numbers

If two values of different numeric data types operate on one another, ESQL/C converts the values to the data type that Figure 3-11 indicates and then performs the operation.

Figure 3-11

Operands DEC FLOAT INT SERIAL SMALLFLOAT SMALLINT
DEC DEC DEC DEC DEC DEC DEC
FLOAT DEC FLOAT FLOAT FLOAT FLOAT FLOAT
INT DEC FLOAT INT INT FLOAT INT
SERIAL DEC FLOAT INT INT FLOAT INT
SMALLFLOAT DEC FLOAT FLOAT FLOAT FLOAT FLOAT
SMALLINT DEC FLOAT INT INT FLOAT INT
Data Types for Which ESQL/C Carries Out Numeric Operations

Figure 3-11 shows that if ESQL/C performs an operation between an operand with a data type of FLOAT and a second operand with a data type of DECIMAL (DEC), ESQL/C generates a result that has a DECIMAL data type. For more information about DECIMAL values, see the next section, "Using Operations That Involve a Decimal Value."

Using Operations That Involve a Decimal Value

The following table shows the numeric data types. Database columns use the SQL data types, and ESQL/C host variables use the corresponding ESQL/C data types.

SQL Data Type ESQL/C Data Type
INTEGER 4-byte integer
SMALLINT short integer
DECIMAL decimal
MONEY decimal
FLOAT double
SMALLFLOAT float

When ESQL/C performs arithmetic operations on operands with numeric data types and one of the operands has a decimal value (an SQL data type of DECIMAL or an ESQL/C data type of decimal), ESQL/C converts each operand and the result to a decimal value.

An SQL DECIMAL data type has the format DECIMAL(p,s), where p and s represent the following parameters:

The ESQL/C decimal data type keeps track of precision and scale differently from the SQL DECIMAL data type. For simplicity, this section uses the format of the SQL DECIMAL data type to describe how ESQL/C performs data conversion for arithmetic operations that involve a decimal value. However, this same data-conversion information applies to arithmetic operations that involve an ESQL/C decimal host variable.

For more information on how the decimal data type tracks precision and scale, see The decimal Structure.

Converting the Non-DECIMAL Numeric Operand

ESQL/C converts all operands that are not already DECIMAL (or decimal) to DECIMAL before it performs the arithmetic operation. The following table shows the precision and scale that ESQL/C uses for the non-DECIMAL operand.

Operand Type Convert To
FLOAT DECIMAL(17)
SMALLFLOAT DECIMAL(9)
INTEGER DECIMAL(10,0)
SMALLINT DECIMAL(5,0)

ESQL/C does not consider leading or trailing zeros as significant digits. Leading or trailing zeros do not contribute to the determination of precision and scale. If the operation is addition or subtraction, ESQL/C adds trailing zeros to the operand with the smaller scale until the scales are equal.

Obtaining the DECIMAL Data Type of the Arithmetic Result

The precision and scale of the arithmetic result depend on the precision and scale of the operands and on whether one of the operands is a floating-point decimal, as follows:

If the data type of the result of an arithmetic operation requires the loss of significant digits, ESQL/C reports an error.


INFORMIX-ESQL/C Programmer's Manual, Version 9.21
Copyright © 1999, Informix Software, Inc. All rights reserved