Data type conversion
The following example
retrieves the average of a DECIMAL column, which is itself a DECIMAL
value. However, the host variable into which the average of the DECIMAL
column is placed is not required to have that data type.
EXEC SQL SELECT avg (total_price) into :avg_price
FROM items;
The declaration of the receiving variable avg_price in the
previous example of code
is not shown. The declaration could be any one of the following definitions:
int avg_price;
double avg_price;
char avg_price[16];
dec_t avg_price; /* typedef of decimal number structure */
The data type of each host variable that is used in a statement is noted and passed to the database server with the statement. The database server does its best to convert column data into the form that the receiving variables use. Almost any conversion is allowed, although some conversions cause a precision loss. The results of the preceding example differ, depending on the data type of the receiving host variable, as the following table shows.
Data type | Result |
---|---|
FLOAT | The database server converts the decimal result to FLOAT, possibly truncating some fractional digits. If the magnitude of a decimal exceeds the maximum magnitude of the FLOAT format, an error is returned. |
INTEGER | The database server converts the result to INTEGER, truncating fractional digits if necessary. If the integer part of the converted number does not fit the receiving variable, an error occurs. |
CHARACTER | The database server converts the decimal value to a CHARACTER string. If the string is too long for the receiving variable, it is truncated. The second field of SQLWARN is set to W and the value in the SQLSTATE variable is 01004. |