MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Truncation occurs in the calucated column

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
viji
Beginner


Joined: 03 Dec 2002
Posts: 17
Topics: 11

PostPosted: Tue Jul 22, 2003 7:14 am    Post subject: Truncation occurs in the calucated column Reply with quote

Hi,
I have two columns A & B defined as Integer in a DB2 table. Now i need to calculate (A/B)*100......When I do this the decimal part gets truncated!!! this could be because the original column(A & B) was defined as "INTEGER" . How can I overcome this.

For example when A=19 & B=22 then
I need (A/B)*100 = 86.36 But I am getting 80

Thanks
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12367
Topics: 75
Location: San Jose

PostPosted: Tue Jul 22, 2003 10:22 am    Post subject: Reply with quote

Viji,

If both operands of an arithmetic operator are integers, the operation is performed in binary and the result is a large integer. Any remainder of division is lost. The result of an integer arithmetic operation (including unary minus) must be within the range of large integers.

So inorder to have a decimal portion convert one of the integer to a decimal and you will get the result as a decimal

try this
Code:

Select ( A / DECIMAL(B)) * 100
        FROM TABLE
;


or
Code:

SELECT ( 19 / DECIMAL(22 )) * 100   
      FROM                           
      SYSIBM.SYSDUMMY1               
      ;                             

The result of the above query would be 86.3636363600

Hope this helps...

cheers

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12367
Topics: 75
Location: San Jose

PostPosted: Tue Jul 22, 2003 5:21 pm    Post subject: Reply with quote

Viji,

You also need to be careful about the "division by zero" errors. You can use a CASE statement to avoid the division by zero error.The following sql will initialize the divide value to a null(value unknown) if the contents of the column B is zero. If the contents of b is any thing other than zero , it performs the division and puts the value with the decimals.

Code:

SELECT (CASE B
       WHEN 0 THEN NULL
       ELSE
       (A / DECIMAL(B)) * 100
       END)
       FROM
       TABLE
       ;


Hope this helps...

cheers

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
viji
Beginner


Joined: 03 Dec 2002
Posts: 17
Topics: 11

PostPosted: Wed Jul 23, 2003 12:11 am    Post subject: Reply with quote

Thanks Kolusu, Your solution solved my problem!!!!!!
Thanks once again.... Razz
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group