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 > Numeric check

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-31-08, 17:40
prem18 prem18 is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Numeric check

Is there any way to validate numeric characters in db2?
__________________
*** Prem ***
Oracle Certified Associate - SQL & PL/SQL
Reply With Quote
  #2 (permalink)  
Old 11-01-08, 10:57
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by prem18
Is there any way to validate numeric characters in db2?
I would CAST() them to a numeric type (DECFLOAT or DECIMAL or INT) and inspect the potential error message.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #3 (permalink)  
Old 11-01-08, 11:10
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
E.g., on DB2 v8 for z/OS, the following casts return:
Code:
cast('123.45' AS dec(10,3))              SQLCODE=   0
cast('123.45' AS dec(5,3))               SQLCODE=-420
cast('123.45' AS int)                    SQLCODE=-420
cast('invalid' AS int)                   SQLCODE=-420
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #4 (permalink)  
Old 11-01-08, 13:51
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
On DB2 LUW, you can put the cast into a stored procedure, add an exception handler for the respective SQLSTATE and then call the stored proc from a UDF so that you can use the function in your SQL statements. Search for "Serge Rielau" and "soft_int" in your favorite search engine.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 11-01-08, 20:39
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Reply With Quote
  #6 (permalink)  
Old 11-02-08, 02:16
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Especially note the possibilities of the DECFLOAT datatype:
it allows for numeric expressions like e.g. "123e-7" or even "+Inf" or "NaN".
Code:
CAST('1.2345e56' AS DECFLOAT)    --> SQLCODE = 0
CAST('abcdef' AS DECFLOAT)       --> SQLCODE = -420
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #7 (permalink)  
Old 11-03-08, 15:08
prem18 prem18 is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Smile

Thanks for everyone. I handled it by capturing the sql code -420 as mentioned above.
__________________
*** Prem ***
Oracle Certified Associate - SQL & PL/SQL
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