4. Understanding the Elements of SQL Statements : SQL Operations : Arithmetic Operations : Arithmetic Operations on Decimal Data Types
 
Share this page                  
Arithmetic Operations on Decimal Data Types
Standard decimal handling rules are used by default (the decimal_rule parameter in config.dat is set to Standard rather than Classic).
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 Standard result can be determined, as shown in the following table:
Operation
Result Precision
Result Scale
Addition and subtraction
Largest number of fractional digits plus largest number of non-fractional digits + 1
Largest number of fractional digits
Multiplication
Total of input precisions
Total of input scales
Division
Dividend non-fractional digits, plus divisor scale, plus result scale
Dividend scale, plus divisor precision, plus one; but at least 10
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 (largest number of fractional digits) + 3 (largest number of non-fractional digits) + 1 = 7.
Scale = 3. The first operand has the largest number of digits to the right of the decimal point.
Result: 0569.124
If exponentiation is performed on a decimal value, the resulting data type is float.
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.
Input Precision and Scale
Classic Result
Standard Result
(39,10) + (39,5)
(39,10)
(39,5)
(14,3) * (14,3) * (14,3) * (4,1)
(39,10)
(39,3)
(39,20) * (39,20)
(39,39)
(39,4)
(5,1) / (3,1)
(39,33)
(15,10)
(14,4) / (12,2)
(39,26)
(29,17)
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.
Specify Error Handling for Arithmetic
To specify error handling for numeric overflow, underflow, and division by zero, use one of these methods:
Set the ‑NUMERIC_OVERFLOW=option flag:
On the CONNECT statement or
On the command line for commands that accept SQL option flags. For details about SQL option flags, see the sql command.
Issue the SET NUMERIC_OVERFLOW option (see Numeric_overflow) statement.
where option is one of the following:
IGNORE
Issues no error
WARN
Issues a warning message
FAIL
(Default) Issues an error message and aborts the statements that caused the error. To obtain ANSI‑compliant behavior, specify this option (or omit the ‑NUMERIC_OVERFLOW flag).