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 > Query on integer values in a mixed table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-08, 10:01
PKPChuck PKPChuck is offline
Registered User
 
Join Date: Sep 2006
Location: Columbus, OH
Posts: 64
Query on integer values in a mixed table

AIX 5L
DB2 8.2

Here is the problem I have.


I have a table with a column that contains strings and numbers.

Ex:

90
85
lt30
EMPTY
15
10

I want to pull back records that are greater than 80
select * from tableA where INT(col1) > 80

but as soon as the query reaches a string that can't be converted to an Integer it bombs out

SQL0420N Invalid character found in a character string argument of the
function "INTEGER". SQLSTATE=22018

Is there a way through the WHERE clause, INT conversion, CASE stmt, I can ignore the string as another record that evaluates false, so the query completes with a result set of

90
85


Thanks in advance,

Charlie
Reply With Quote
  #2 (permalink)  
Old 10-09-08, 10:39
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Code:
...where translate(col1,'','0123456789') = ''
Reply With Quote
  #3 (permalink)  
Old 10-09-08, 14:45
PKPChuck PKPChuck is offline
Registered User
 
Join Date: Sep 2006
Location: Columbus, OH
Posts: 64
Still not sure how this works, but it works beautifully. Thanks.
Reply With Quote
  #4 (permalink)  
Old 10-09-08, 15:27
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
It replaces every digit in the string with a blank. If what's left is a blank string, it means there was nothing but digits, therefore it can be cast to an integer without errors.
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