If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > CAST a Decimal

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-13-08, 13:28
prem18 prem18 is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Question CAST a Decimal

A QUICK QUESTION.

Is it not possible to cast a decimal(12) to decimal(7,2)?

Its throwing -413 error.
__________________
*** Prem ***
Oracle Certified Associate - SQL & PL/SQL
Reply With Quote
  #2 (permalink)  
Old 11-13-08, 13:50
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Not if the number exceeds 9999999.99

Andy
Reply With Quote
  #3 (permalink)  
Old 11-13-08, 14:03
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Yes, if the values was less than 100000.

Code:
------------------------------ Commands Entered ------------------------------
SELECT d                        AS "decimal(12)"
     , CAST(d AS decimal(7,2) ) AS "decimal(7,2)"
  FROM (VALUES CAST(99999.9 AS decimal(12)) ) AS d(d)
;
------------------------------------------------------------------------------

decimal(12)    decimal(7,2)
-------------- ------------
        99999.     99999.00

  1 record(s) selected.


------------------------------ Commands Entered ------------------------------
SELECT d                        AS "decimal(12)"
     , CAST(d AS decimal(7,2) ) AS "decimal(7,2)"
  FROM (VALUES CAST(100000. AS decimal(12)) ) AS d(d)
;
------------------------------------------------------------------------------

decimal(12)    decimal(7,2)
-------------- ------------
SQL0413N  Overflow occurred during numeric data type conversion.  
SQLSTATE=22003

<snipped some lines>

 sqlcode :  -413 

 sqlstate :  22003
Reply With Quote
  #4 (permalink)  
Old 11-13-08, 21:08
nick.ncs nick.ncs is offline
Registered User
 
Join Date: May 2007
Location: somewhere in dbforums
Posts: 221
basically your decimal(12) can hold much more data than decimal(7,2) and since you are narrowing down [ i.e. going from a higher container decimal(12) to a lower sized container decimal(7,2) ] you cannot input anything more than 9999999.99 for then it i'll be something like trying to pour water from a 2 liter jug into a 1 liter jug; obviously there'll be an overflow

one thing you can try out is if value is less than 9999999.99 then convert else handle error
Quote:
case (when value < 9999999.99)
then CAST
else
HANDLE ERROR
end
__________________
IBM Certified Database Associate, DB2 9 for LUW
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On