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 > Is there a "CONTAINS" in SQL?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-18-06, 00:56
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Is there a "CONTAINS" in SQL?

I want to "match" by two columns. So something like:

SELECT * FROM tablea A, tableb B WHERE B.msglist CONTAINS A.msg;



A.msg looks like
MSG1
MSG2
MSG3
etc

and B.msglist looks like
(MSG1)&&(MSG3)&&(MSG9)
(MSG1)&&(MSG2)&&(MSG6)
(MSG8)&&(MSG7)&&(MSG3)
etc


is there any way to do that?
Reply With Quote
  #2 (permalink)  
Old 01-18-06, 08:49
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
String handling isn't very consistent between different versions of SQL. The exact syntax will depend on what SQL engine you are using, but it is definitely possible.

If you can settle for a "loose match" that might be confused by special characters like _ and %, you could use concatenate percent signs at the begining and end of your "search for" column, and use that as the LIKE pattern. This is reasonably portable, but it still depends on the string concatenation syntax (which varies from one SQL engine to another).

-PatP
Reply With Quote
  #3 (permalink)  
Old 01-18-06, 17:36
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Thanks. I'm on DB2

So something like:

SELECT * FROM tablea A, tableb B WHERE B.msglist LIKE % + A.msg + %;

I dont even know how to concatenate strings in db2 =\
Reply With Quote
  #4 (permalink)  
Old 01-18-06, 19:03
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I'm moving this thread to the DB2 forum, since you'll get a lot more relevant comments there than in the "pure" SQL forum.

I'd use:
Code:
SELECT *
   FROM tablea A, tableb B
   WHERE B.msglist LIKE '%' CONCAT A.msg CONCAT '%';
-PatP
Reply With Quote
  #5 (permalink)  
Old 01-19-06, 02:25
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
I'm afraid, you cannot use a table column at the right side of a LIKE. ( at least in DB2 for z/OS you can't )

but a

... WHERE LOCATE(A.msg , b.msglist ) > 0

should work.
LOCATE returns the position of parameter1 in parameter2 if found, and zero if parameter2 does not contain parameter1
Reply With Quote
  #6 (permalink)  
Old 01-19-06, 17:54
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by umayer
I'm afraid, you cannot use a table column at the right side of a LIKE. ( at least in DB2 for z/OS you can't )
Really? I thought expressions as the LIKE pattern were supported in DB2 7.2 and later across all platforms. Gee, I learned something, so I can go home now!

-PatP
Reply With Quote
  #7 (permalink)  
Old 01-20-06, 01:22
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
LIKE itself is supported in DB2 z/OS, of course.

WHERE column-name LIKE expression

but expression must be:
a special register or
a host-variable or
a string-constant or
a result of a function based on the above.

expression must not be (or contain) a column-name
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