问题
Can somebody please explain the following result I get in Teradata:
SELECT TYPE(CAST (2.3 AS DECIMAL(18,4)) * CAST (2.3 AS DECIMAL(18,4)) )
The result is:
DECIMAL(18,8)
I was expecting DECIMAL(18,4)
回答1:
Every DBMS has it's own rules regarding calculations involving Decimals.
Teradata's basic rules are: When you add/substract/divide DECIMALs the resulting fractional precision is the greater of both operands, e.g. dec(10,2) + dec(10,4) = dec(xx,4)
But when you multiply the fractional digits are added, e.g.dec(10,2) * dec(10,4) = dec(xx,6)
The overall precision has some more rules (some depending on the dbscontrol MAxDecimal setting).
And then there's the most important rule, people tend to forget: After each calculation the result is rounded to this precision.
sel 2.0/3.00 * 100, 100*2.0/3.00;
*** Query completed. One row found. 2 columns returned.
*** Total elapsed time was 1 second.
((2.0/3.00)*100) ((100*2.0)/3.00)
----------------- -----------------
67.00 66.67
Dieter
来源:https://stackoverflow.com/questions/17574015/decimal-types-in-teradata