DECIMAL Types in Teradata

拜拜、爱过 提交于 2019-12-14 00:36:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!