How to define a DECIMAL datatype in DB2 for z/OS

Young DBA's and developers always have this confusion while defining the DECIMAL datatype in DB2. Well I had that problem as well and here is how I overcame it a long time ago,


Requirement : I need a column CURRENCY which can accomdate this figure : $ 1,000,000.00 on a DB2 for z/OS table . This post is designed to help you understand the ' how to ' of defining a DECIMAL datatype on a DB2 table. Let's now focus the requirement on defining $ 1,000,000.00 as a decimal datatype


Break your request to 3 easy findings :


1) As we can see 1,000,000 is on the left hand side of the period ( dot ) and it accounts for 7 digits of the number, this is called precision of the decimal data type.
2) We see a period ( a dot ' . ' ) after the 7 digit number, well we will call it a dot.
3) We also see 00 on the right hand side of the period ( dot ) which accounts for 2 more digits of the number, it is called the scale.


7 ( precision ) + 2 ( scale ) = 9 : Straight forward DECIMAL ( 9 , ? ) - What about the ? ( question mark ). Well that's the scale which is 2 in our case, so it makes up as DECIMAL ( 9 , 2 )


To define $ 1,000,000.00 in DB2 you need to define the column datatype as DECIMAL ( 9 , 2 )


Note :  The usual mistake commited in interpreation of 1,000,000.00 will be reading the figure as DECIMAL(7,2) which is incorrect.

No comments:

Post a Comment