![]() |
|
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.
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:
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
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.
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.
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.
For more information on the BOOLEAN data type, see Chapter 5, Working with Numeric Data Types.
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.
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.
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:
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
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."
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 OperandESQL/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 ResultThe 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:
Figure 3-12
Precision and Scale of Fixed-Decimal Arithmetic Results
If the data type of the result of an arithmetic operation requires the loss of significant digits, ESQL/C reports an error.