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 > Like operator in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-28-10, 16:38
nikhilk nikhilk is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
Like operator in DB2

Hello,

I am new to this forums and new to DB2 too.

I need some help regarding the LIKE operator in DB2.

Here is my requirement.
I have a table that has values like:
1234
12345
12345AB
1234AB
AB1234CD
1234ABCD

I am supposed to write a query that looks for '1234' and return these values:
1234
1234AB
AB1234CD
1234ABCD

Any help is greatly appreciated.

Thanks,
Nikhil
Reply With Quote
  #2 (permalink)  
Old 01-28-10, 16:45
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by nikhilk View Post
12345
12345AB
Nikhil
Why do these two rows not qualify?

Andy
Reply With Quote
  #3 (permalink)  
Old 01-28-10, 16:48
nikhilk nikhilk is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
Andy,

I am looking for row values that has the number '1234' in it along with alphabets but not numbers, at the beginning or at the end.

Nikhil.
Reply With Quote
  #4 (permalink)  
Old 01-28-10, 16:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Something along these lines:
Code:
select 
..
where  int(trim(translate(yourcolumn,'','ABCDEFGHIJKLMNOPQRSTUVWXYZ'))) = 1234
Reply With Quote
  #5 (permalink)  
Old 01-29-10, 09:30
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
Nick, out of curiosity have you ever evaluated what it does to performance using translate? I have never had to use it.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
Reply With Quote
  #6 (permalink)  
Old 01-29-10, 10:43
harlinseritt harlinseritt is offline
Registered User
 
Join Date: Jan 2010
Location: Atlanta, GA
Posts: 2
Why aren't you able to do:

select <column_name> from <table_name> where <column_name> like '&#37;1234%';

Maybe I'm missing something.
Reply With Quote
  #7 (permalink)  
Old 01-29-10, 10:55
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Nikhil's replay to Andy's question:

Quote:
I am looking for row values that has the number '1234' in it along with alphabets but not numbers, at the beginning or at the end.
The 4 numbers have to be can NOT have another number immediately before or after it. It has to be nothing or an alpha character.

Using %1234% would return 12345 and 12345AB and both of these violate the restriction.
Reply With Quote
  #8 (permalink)  
Old 01-30-10, 13:15
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What's with rows like 0ABC1234def567? If such a row should qualify, the TRANSLATE approach won't work either.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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