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 > Need help with LIKE/CONTAINS

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-23-04, 14:44
edrenckh edrenckh is offline
Registered User
 
Join Date: Jun 2004
Posts: 11
Need help with LIKE/CONTAINS

Runing DB2, Mainframe, V7

SELECT
T.MERCH_NM
, ORD.ORDER_NM
FROM
(SELECT
ACCT_NBR
, UCASE('''' || '%' || RTRIM(O.MERCH_NM) || '%' || '''')
AS "ORDER_NM"
FROM
ORDER O
WHERE
O.ACCT_NBR = 'ABC' )
AS ORD
, TRANSACTION T
WHERE
T.ACCT_NBR = ORD.ACCT_NBR
AND UCASE(T.MERCH_NM) LIKE ORD.ORDER_NM

DB2 is having an issue with the LIKE statement.

I want to say T.MERCH_NM LIKE '%O.MERCH_NM%', but use the value of O.MERCH_NM, not the string "O.MERCH_NM".

You cannot do nested statements with LIKE (why not IBM....?).

Is there another way to do a contains?? Any thoughts?
Reply With Quote
  #2 (permalink)  
Old 08-23-04, 14:58
RonD RonD is offline
Registered User
 
Join Date: Aug 2004
Posts: 33
You could try using a common table expression instead of a nested querry.

With
ORD (ACCT_NBR, ORDER_NM) AS
(SELECT ACCT_NBR , UCASE('''' || '%' || RTRIM(O.MERCH_NM) || '%' || '''')
AS "ORDER_NM"
FROM ORDER O
WHERE
O.ACCT_NBR = 'ABC' )

SELECT
T.MERCH_NM
, ORD.ORDER_NM
FROM ORD, TRANSACTION T
WHERE
T.ACCT_NBR = ORD.ACCT_NBR
AND UCASE(T.MERCH_NM) LIKE ORDER_NM

I hope this works for you.
Reply With Quote
  #3 (permalink)  
Old 08-23-04, 15:34
edrenckh edrenckh is offline
Registered User
 
Join Date: Jun 2004
Posts: 11
I'm running V7, CTE's are a V8 thing.....
Reply With Quote
  #4 (permalink)  
Old 08-23-04, 15:54
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Try something like this:

SELECT
T.MERCH_NM
, ORD.ORDER_NM
FROM
(SELECT
ACCT_NBR
, UCASE(RTRIM(O.MERCH_NM))
AS "ORDER_NM"
FROM
ORDER O
WHERE
O.ACCT_NBR = 'ABC' )
AS ORD
, TRANSACTION T
WHERE
T.ACCT_NBR = ORD.ACCT_NBR
AND UCASE(T.MERCH_NM) LIKE '%' || ORD.ORDER_NM || '%'

not quite sure how to concatenate the wildcard with the column value so you may have to play with the syntax.
Reply With Quote
  #5 (permalink)  
Old 08-23-04, 16:25
edrenckh edrenckh is offline
Registered User
 
Join Date: Jun 2004
Posts: 11
Nope again....

I even tried to create a GLOBAL TEMPORARY, load it with the value, confirmed that it was a correct value in the temp, then joining TRANSACTION and the GLOBAL TEMP. The LIKE still wants a literal or a host variable or ??.

Feel free to send more ideas....
Reply With Quote
  #6 (permalink)  
Old 08-23-04, 17:03
edrenckh edrenckh is offline
Registered User
 
Join Date: Jun 2004
Posts: 11
I think that DB2 will not allow a variable on the right side of the LIKE.
Reply With Quote
  #7 (permalink)  
Old 08-23-04, 17:15
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Are you looking for transactions whose MERCHANT_NM is included somewhere within the order's MERCHANT_NM ? Are they different sized fields?
Reply With Quote
  #8 (permalink)  
Old 08-24-04, 03:57
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Why don't you just use locate?

AND LOCATE(UPPER(O.MERCH_NUM), UPPER(T.MERCH_NUM)) != 0


???
Reply With Quote
  #9 (permalink)  
Old 08-24-04, 11:34
edrenckh edrenckh is offline
Registered User
 
Join Date: Jun 2004
Posts: 11
LOCATE seems to work. I had to RTRIM both sides first, without the RTRIM, no go.

Thanks!!!!
Reply With Quote
  #10 (permalink)  
Old 08-24-04, 12:34
RonD RonD is offline
Registered User
 
Join Date: Aug 2004
Posts: 33
Exclamation

Common table expressions can be used in DB2 ver 7. We are currently using them, all though I am on DB2 UDB for Windows.
Reply With Quote
  #11 (permalink)  
Old 08-24-04, 12:42
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally Posted by RonD
Common table expressions can be used in DB2 ver 7. We are currently using them, all though I am on DB2 UDB for Windows.
I concur (and I'm on AIX)
Reply With Quote
  #12 (permalink)  
Old 08-24-04, 13:43
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
CTE on mainframes was introduced only with V8

Cheers
sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
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