| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| viji Beginner
 
 
 Joined: 03 Dec 2002
 Posts: 17
 Topics: 11
 
 
 | 
			
				|  Posted: Tue Jul 22, 2003 7:14 am    Post subject: Truncation occurs in the calucated column |   |  
				| 
 |  
				| 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 |  | 
	
		|  | 
	
		| kolusu Site Admin
 
  
 
 Joined: 26 Nov 2002
 Posts: 12394
 Topics: 75
 Location: San Jose
 
 | 
			
				|  Posted: Tue Jul 22, 2003 10:22 am    Post subject: |   |  
				| 
 |  
				| 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 |  | 
	
		|  | 
	
		| kolusu Site Admin
 
  
 
 Joined: 26 Nov 2002
 Posts: 12394
 Topics: 75
 Location: San Jose
 
 | 
			
				|  Posted: Tue Jul 22, 2003 5:21 pm    Post subject: |   |  
				| 
 |  
				| 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 |  | 
	
		|  | 
	
		| viji Beginner
 
 
 Joined: 03 Dec 2002
 Posts: 17
 Topics: 11
 
 
 | 
			
				|  Posted: Wed Jul 23, 2003 12:11 am    Post subject: |   |  
				| 
 |  
				| Thanks Kolusu, Your solution solved my problem!!!!!! Thanks once again....
  |  | 
	
		| Back to top |  | 
	
		|  | 
	
		|  | 
	
		|  |