help using 'like' condtions

  • Hi ,

    I would like to join two tables A and B. Table A has a column Drug and table B has column drug1. But there is no matching name in both the columns.

    TABA TabB

    Drug Drug1

    acid 123acidtc

    base 987basegt

    arythom 98arythomgt

    If you observe in the above example you can see Both the columns match if you use like condtions.

    Can you help me out how to use like condtions in this kind of situation.

  • Try something like this:select * from tableA

    join tableB on tableA.drug like '%' + tableB.drug1 + '%'

    I think this is the correct syntax. (Someone correct me if I'm wrong.)

    Also, keep in mind that this may not give you a one-to-one table join.

    Hope this helps!

    +--------------------------------------------------------------------------------------+
    Check out my blog at https://pianorayk.wordpress.com/

  • Ray K (6/23/2010)


    Try something like this:select * from tableA

    join tableB on tableA.drug like '%' + tableB.drug1 + '%'

    I think this is the correct syntax. (Someone correct me if I'm wrong.)

    Also, keep in mind that this may not give you a one-to-one table join.

    Hope this helps!

    Looks good to me and you're also correct about it likely (pun on words, sorry) not producing a one-to-one join.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • You're also likely (continuing the pun) to see some pretty poor performance - with the leading wildcard, you won't be able to utilize an index.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply