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 > select only records containing alphabets

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-09, 20:30
thomast thomast is offline
Registered User
 
Join Date: Dec 2009
Posts: 7
select only records containing alphabets

Hi all, I am new to DB2 and am looking for some inspiration on this forum.

I am having a bit of a problem with an issue and hope that the kind souls here can help me with it.

I needed to select records that contain only alphabets. For example,
Column A
X13568J
16879
KT66980
ALPHA
3688908I
235IKL86
RICHTEXT

The query should only extract 'ALPHA' and 'RICHTEXT'.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-01-09, 20:35
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Something like
Code:
... where translate(columna, '', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') = ''
Don't expect this to be fast.
Reply With Quote
  #3 (permalink)  
Old 12-01-09, 20:37
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can use the TRANSLATE function to translate all letters to an empty string and then filter out all rows where the result of the TRANSLATE on the whole string is not empty.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 12-01-09, 21:47
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Lightbulb small correction

Quote:
Originally Posted by n_i View Post
Something like
Code:
... where translate(columna, '', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') = ''
Don't expect this to be fast.
With small correction, if you want:

Code:
... 
where 
columna = replace(columna, ' ', '?') 
and
translate(columna, ' ', 
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
|| Lcase('ABCDEFGHIJKLMNOPQRSTUVWXYZ') ) = ' '
...

Kara

Last edited by DB2Plus; 12-01-09 at 21:58.
Reply With Quote
  #5 (permalink)  
Old 12-01-09, 22:03
thomast thomast is offline
Registered User
 
Join Date: Dec 2009
Posts: 7
Thanks to all for the quick response. It works wonderfully though it takes quite a while.
Reply With Quote
  #6 (permalink)  
Old 12-01-09, 23:19
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Lightbulb Faster query

Quote:
Originally Posted by thomast View Post
Thanks to all for the quick response. It works wonderfully though it takes quite a while.
Following query will work much faster:

Code:
select tt.columna
from
(select columna, nullif(columna, replace(columna, ' ', '?')) NRcolumna,
translate(columna, ' ', 
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
|| Lcase('ABCDEFGHIJKLMNOPQRSTUVWXYZ') ) Tcolumna
from your_table  ) tt
where NRcolumna is null and Tcolumna = ' '
Kara
Reply With Quote
  #7 (permalink)  
Old 12-02-09, 01:46
thomast thomast is offline
Registered User
 
Join Date: Dec 2009
Posts: 7
Thanks Kara. I removed the LCASE function as i did not need it.
With your new stt, I realise that it is not able to extract any record. Probably due to 'is null' even though the nested select stt works fine.
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