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 > Using Multiple LIKE parameters in SELECT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-12-04, 12:24
anil4321 anil4321 is offline
Registered User
 
Join Date: Feb 2003
Posts: 20
Using Multiple LIKE parameters in SELECT

I am using DB2 UDB V8.1 with FixPack 5. I have a table test1 with one column col1 and I have the following data in the table:

COL1
----------
C_TEST1
COMMON
C_TEST2
COX CABLE
C__TEST1
C__OMMON
C__TEST2
COX_CABLE
R__TEST1
R__OMMON
R__TEST2
ROX_CABLE

12 record(s) selected.

I want to select all the records which are like C_ and R_.

I am issuing the following command to get the records which are like C_ and R_

1) db2 "select * from test1 where col1 like 'C\_%' or col1 like 'R\_%' ESCAPE '\'"

COL1
----------
R__TEST1
R__OMMON
R__TEST2

3 record(s) selected.

When I issue the command

2) db2 "select * from test1 where col1 like 'R\_%' or col1 like 'C\_%' ESCAPE '\'"

I am getting the following result:

COL1
----------
C_TEST1
C_TEST2
C__TEST1
C__OMMON
C__TEST2

5 record(s) selected.

I am expecting my result set to be UNION of 1) and 2).

Can anybody tell me if I am doing anything wrong or there is a problem with UDB?
Reply With Quote
  #2 (permalink)  
Old 07-12-04, 12:40
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
ESCAPE '\' must be a part of each LIKE predicate:
Code:
select * from test1 where col1 like 'R\_%' ESCAPE '\' or col1 like 'C\_%' ESCAPE '\'
Reply With Quote
  #3 (permalink)  
Old 07-12-04, 17:19
db2guru1 db2guru1 is offline
Registered User
 
Join Date: Aug 2003
Posts: 106
You can also try

SELECT * FROM TEST1 WHERE SUBSTR(COL1,1,2) = 'C_' OR SUBSTR(COL1,1,2) = 'R_'
__________________

You are the creator of your own destiny!
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