Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi All,

I am having tough time doing a simple division in DB2 Stored procedure.

I am using Data studio and working on DB2 z/OS v9.1

My variables:
-------------
value Decimal(18,13)
total_value Decimal(18,13)
Percentage Decimal(12, 9)

Simple Division:
--------------
Percentage= value/total_value * 100

My values:
----------
value = 45.23
total_value = 57.35

Expected Result:
----------------
percentage = 78.866608544

I am getting the percentage as zero. After spending few hrs on browsing I found

http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_decimaldivision.htm[^]

I couldn't able to understand completely but figured out it has to do with precision and scale.
then I played with it and finally i got the result by doing
(cast(value as decimal(15,13)) / cas(total_value as decimal(22,9)) * 100.

Now i found that i am not supposed to cast those values .

Is there any other way of getting result without casting????
Please respond ASAP.......Looking after the solution
Posted
Updated 15-Aug-11 10:19am
v2
Comments
Wendelius 15-Aug-11 16:20pm    
Modified the hyperlink

1 solution

It's been ages since I last used DB2, but what happens if you change the precision for the percentage. So instead of:
Percentage Decimal(12, 9)

use
Percentage Decimal(18, 13)

or higher.
 
Share this answer
 
Comments
YogiG 15-Aug-11 16:33pm    
I tried that but no use..
Wendelius 15-Aug-11 16:42pm    
And have also tried using float instead of decimal for all variables?
YogiG 15-Aug-11 16:58pm    
Hi that helped, I wasn't trying that because I am not supposed to change datatype. Now i used casting instead changing the datatype. Thanks
Wendelius 15-Aug-11 17:24pm    
That's good. Most likely the fixed location of the decimal point caused the truncation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900