IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Usage of like between columns with wild card entries


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
vvmanyam

New User


Joined: 16 Apr 2008
Posts: 86
Location: Bangalore

PostPosted: Mon Jun 22, 2009 8:03 pm
Reply with quote

Hi ,
I have two tables which needs to be joined based on COL2. The issue here is One of the tables has wild card entries. How to compare these 2 columns while joining.
Sample Table 1:
Code:

COL1   COL2   
-----  ----- 
AAA    A23   
BBB    A34   
CCC    BB0   
DDD    C10   
EEE    C04   

Table2:
Code:

Col2   
-----   
A%%
C1%


Output of the query should be:
Code:

COL1   COL2   
-----  ----- 
AAA    A23   
BBB    A34     
DDD    C10   

'%' will be cosidered as wild card and any character can be replaced for matching
Thanks,
Balu
Back to top
View user's profile Send private message
vvmanyam

New User


Joined: 16 Apr 2008
Posts: 86
Location: Bangalore

PostPosted: Mon Jun 22, 2009 8:05 pm
Reply with quote

Please note that we are using 'DB2 version 7.2'

Thanks,
Balu
Back to top
View user's profile Send private message
vvmanyam

New User


Joined: 16 Apr 2008
Posts: 86
Location: Bangalore

PostPosted: Wed Jun 24, 2009 9:42 am
Reply with quote

I am surprised!!
This is first time in last one year, I have not received any replies for my posts!!
This is my first post in DB2 forum icon_smile.gif

I finally used Sort to generate the multiple "LIKE" condition query in runtime and submit that query!!

Can this requirement be achieved in a single query?

Thanks,
Balu
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jun 24, 2009 12:20 pm
Reply with quote

Well, you can't join since your relationship is LIKE and not EQUAL or NOT EQUAL.

what you proposed is a SELECT FROM table a WHERE A.COL2 LIKE (SELECT COL2 FROM table b).

did not have time to use my own machine time experimenting, did you?
Back to top
View user's profile Send private message
vvmanyam

New User


Joined: 16 Apr 2008
Posts: 86
Location: Bangalore

PostPosted: Wed Jun 24, 2009 1:07 pm
Reply with quote

This is what I have tried!!
Code:

SELECT A.COL1 A.COL2             
FROM TABLE1 A, TABLE2 B           
WHERE A.COL2 LIKE(SELECT COL2 FROM TABLE2) 

This is not working
So, I had to use below query for that!!
Code:

SELECT A.COL1 A.COL2             
FROM TABLE1 A           
WHERE                             
    (A.COL2 LIKE 'A%%' OR         
     A.COL2 LIKE 'C1%');         

Since the no of records in table2 is not constant, I have first extracted the Table2 content and generated the Where condition with list of LIKE commands dynamically using 'SORT' step and concatenated with the remaining query!!

Thanks,
Balu
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jun 24, 2009 1:56 pm
Reply with quote

from the book:
Quote:

LIKE pattern-expression An expression that specifies the pattern of characters to be matched. The expression can be specified by any one of the following:
  • A constant
  • A special register
  • A host variable (including a LOB locator variable)
  • A scalar function whose arguments are any of the above (though nested function invocations cannot be used)
  • A CAST specification whose arguments are any of the above
  • An expression that concatenates (using CONCAT or ||) any of the above


The expression must also meet these restrictions:
  • The maximum length of pattern-expression must not be larger than 4000 bytes. Predicates
  • If a host variable is used in pattern-expression, the host variable must be defined in accordance with the rules for declaring string host variables and must not be a structure.
  • If escape-expression is specified, pattern-expression must not contain the escape character identified by escape-expression except when immediately followed by the escape character, '%', or '_'. For example, if '+' is the escape character, any occurrences of '+' other than '++', '+_', or '+%' in the pattern is an error


the bold was added by me. Though you can generate IN-lists, there apparently is no LIKE-list capability.

either you
  • build one (1) query as you are doing (and hope table 2 does not contain many rows)
  • you have a loop, using a CURSOR on table 2 (or rowset retrieval)
    • to access and build your host-structure pattern-expressions
    • use a second cursor (or rowset retrieval) to retrieve matching rows from table 1 for each pattern-expression.


maybe others read the manual and decided not to answer you.
I knew that you can't have LIKE-lists,
but was intrigued by the potential of the correlated sub-query,
but rechecking the manual tells me that you cannot reference a pattern-expression contained in a column.
Back to top
View user's profile Send private message
vvmanyam

New User


Joined: 16 Apr 2008
Posts: 86
Location: Bangalore

PostPosted: Wed Jun 24, 2009 2:16 pm
Reply with quote

I thank you for all the information provided!

The second solution is a feasible one, but I have to code this in a JCL, which is not possible.

I am going with first solution itself as my second table will have maximum of 10 records.


Thanks,
Balu
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts JCL sort card - get first day and las... JCL & VSAM 9
No new posts Remote Unload of CLOB Columns DB2 6
No new posts Increase the number of columns in the... IBM Tools 3
No new posts DB2 Views with Multiple SQL & Col... DB2 8
No new posts STEM usage in REXX CLIST & REXX 14
Search our Forums:

Back to Top