30 Jul 18 · npack ·       Add to Favorites   Report

DECIMAL datatype behavior while doing calculations in Teradata

Scenario: Look at the following result we get in Teradata. when multiplying two decimal the result data has double decimal precision

SELECT TYPE(CAST (2.3 AS DECIMAL(18,4)) * CAST (2.3 AS DECIMAL(18,4))  )

The result is:

DECIMAL(18,8)
  • 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)
  • And then there's the most important rule, people tend to forget: After each calculation the result is rounded to this precision

Example:

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


npack

posted on 30 Jul 18

Read great educational content like this and a lot more !

Members get free exclusive access to content, new courses, and discounts. Signup for a free account to write a post / comment / upvote posts. Creating an account takes less than 5 seconds and you can start earning badges & points too

Copied