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 w/short but complex sql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-05-04, 20:10
zlek131 zlek131 is offline
Registered User
 
Join Date: Jan 2004
Posts: 7
Lightbulb Need help w/short but complex sql

OK, here it is.

I am joining two tables (A + B)...as part of my where clause I wish to equate one char column from A table to one char column from table B...this has to be done via the "LIKE" clause, here is why...

The char column in table B may or may not have a value in it. When no value is present the column will be set to NULL....

The idea in the SQL is to resolve the LIKE clause to either a valid and specific value from table B's column or wildcard the LIKE clause (% - meaning get everything) when table B's column is NULL

I am running a COALESCE function on the column from table B and want it to return "%" (wildcard) if the value is NULL or the value itself if column is not NULL.

Another words, if the column in table B has a valid value, the where clause should look like this:

WHERE A.col1 LIKE 'valid value from B.col1'

if no valid value is present (NULL) then the where clause should look like this:

WHERE A.col1 LIKE '%'

I have tried all kinds of flavors, but cannot get any of them to work syntactically. Here are some of the things I have tried:

...WHERE a.col1 LIKE COALESCE(b.col1, '%');
...WHERE a.col1 LIKE COALESCE(b.col1, ''%'');
...WHERE a.col1 LIKE ''' || COALESCE(b.col1, '%') || ''';


Any input or feedback is appreciated.

Thanks,
Matt.
Reply With Quote
  #2 (permalink)  
Old 02-05-04, 23:17
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Re: Need help w/short but complex sql

Quote:
Originally posted by zlek131
OK, here it is.

I am joining two tables (A + B)...as part of my where clause I wish to equate one char column from A table to one char column from table B...this has to be done via the "LIKE" clause, here is why...

The char column in table B may or may not have a value in it. When no value is present the column will be set to NULL....

The idea in the SQL is to resolve the LIKE clause to either a valid and specific value from table B's column or wildcard the LIKE clause (% - meaning get everything) when table B's column is NULL

I am running a COALESCE function on the column from table B and want it to return "%" (wildcard) if the value is NULL or the value itself if column is not NULL.

Another words, if the column in table B has a valid value, the where clause should look like this:

WHERE A.col1 LIKE 'valid value from B.col1'

if no valid value is present (NULL) then the where clause should look like this:

WHERE A.col1 LIKE '%'

I have tried all kinds of flavors, but cannot get any of them to work syntactically. Here are some of the things I have tried:

...WHERE a.col1 LIKE COALESCE(b.col1, '%');
...WHERE a.col1 LIKE COALESCE(b.col1, ''%'');
...WHERE a.col1 LIKE ''' || COALESCE(b.col1, '%') || ''';


Any input or feedback is appreciated.

Thanks,
Matt.
I think "LIKE" only takes a literal string. Have you tried this:

... WHERE A.COL1=B.COL1 OR B.COL1 IS NULL....

I think this should do what you want.
Reply With Quote
  #3 (permalink)  
Old 02-06-04, 03:39
Walter Janissen Walter Janissen is offline
Registered User
 
Join Date: Nov 2003
Location: Germany
Posts: 62
Hi

If the LIKE-predicate starts at position 1, you can use the SUBSTR-function instead and if B.col1 is NULL you should use a left outer join. Have you tried that?
Reply With Quote
  #4 (permalink)  
Old 02-06-04, 15:51
zlek131 zlek131 is offline
Registered User
 
Join Date: Jan 2004
Posts: 7
Re: Need help w/short but complex sql

Quote:
Originally posted by n_i
I think "LIKE" only takes a literal string. Have you tried this:

... WHERE A.COL1=B.COL1 OR B.COL1 IS NULL....

I think this should do what you want.

I belive this may work....another person suggested the exact thing this morning. Thank you! Going to test now...Matt.
Reply With Quote
  #5 (permalink)  
Old 02-06-04, 15:58
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: Need help w/short but complex sql

Matt ,

if the string matching string can occur anywhere then you can use the LOCATE function

Eg :

From the SAMPLE Employee table

C:\Documents and Settings\Sathyaram>db2 "select firstnme from employee where locate('SA',firstnme) > 0"

FIRSTNME
------------
SALLY
MASATOSHI
SALVATORE

3 record(s) selected.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 02-06-04, 17:51
zlek131 zlek131 is offline
Registered User
 
Join Date: Jan 2004
Posts: 7
Re: Need help w/short but complex sql

Quote:
Originally posted by zlek131
I belive this may work....another person suggested the exact thing this morning. Thank you! Going to test now...Matt.
Works like a charm. Thanks, Matt.
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