Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2004
    Posts
    7

    Lightbulb Unanswered: 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.

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1

    Re: Need help w/short but complex sql

    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.

  3. #3
    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?

  4. #4
    Join Date
    Jan 2004
    Posts
    7

    Re: Need help w/short but complex sql

    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.

  5. #5
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650

    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.

  6. #6
    Join Date
    Jan 2004
    Posts
    7

    Re: Need help w/short but complex sql

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •