Arithmetic Operations
An arithmetic operation combines two or more expressions using the arithmetic operators to form a resulting numeric expression.
Before performing any arithmetic operation, OpenSQL converts the participating expressions to identical data types. The result is returned as the selected data type. The following sections describe this data type conversion.
Default Type Conversion
When two numeric expressions are combined, the Enterprise Access product converts as necessary to make the data types of the expressions identical and assigns that same data type to the resulting expression. If it is necessary to convert the data type of an expression, the DBMS converts the expression having the data type of lower precedence to that of the higher.
The order of precedence among the numeric data types is, in highest‑to‑lowest order:
• Money
• Float
• Real
• Decimal
• Integer
• Smallint
For example, when OpenSQL operates on an integer and a floating-point number, the integer is converted to a floating-point number. If OpenSQL operates on two integers of different sizes, the smaller is converted to the size of the larger. All conversions are done before the operation is performed.
The following table summarizes the possible results of numeric combinations:
For example, for this expression:
(job.lowsal + 1000) * 12
the first operator (+) combines a float expression (job.lowsal) with a smallint constant (1000). The result is float. The second operator (*) combines the float expression with a smallint constant (12), resulting in a float expression.
For money data type, if the above table conflicts with Host DBMS default type conversion, Host DBMS default type conversion has higher priority.
Arithmetic Operations on Decimal Data Types
In expressions that combine decimal values and return decimal results, the precision (total number of digits) and scale (number of digits to the right of the decimal point) of the result can be determined, as shown in the following table:
If the result precision is larger than 39, the result scale is reduced by the amount of the precision excess, and the result precision is set to 39. The result scale is not reduced below 4 if the input scales are 4 or more; if both input scales are less than 4, the result scale is not reduced to less than the larger.
For example, in the following decimal addition operation:
1.234 + 567.89
the scale and precision of the result is calculated as follows:
Precision = 7
Calculated as 3 (larger number of fractional digits) + 3 (larger number of non‑fractional digits) + 1 = 7
Scale = 3
The first operand has the larger number of digits to the right of the decimal point
Result:
0569.124
Note: If the result of arithmetic using decimal data exceeds the declared precision or scale of the column to which it is assigned, OpenSQL truncates the result and does not issue an error.
Comparison of Decimal Handling Settings
Decimal arithmetic is handled according to the setting on the decimal_rule parameter in config.dat--either Classic or Standard.
The following examples compare Classic and Standard decimal handling. Given the input precision and scale, the result precision and scale for Classic and Standard settings are shown.
The Classic rules follow the SQL Standard specifications for result scale, even when the precision has to be limited to the maximum of 39. The effect is that in a complex expression, the result scale tends to be large, which reduces the number of available non-fractional digits and makes numeric overflow more likely.
The Standard rules reduce both precision and scale when precision gets too large, which favors non-fractional digits at the expense of fractional digits and makes numeric overflow less likely.
Last modified date: 04/03/2024