Can you match text rows using LIKE in a join?

  • Dear Forum,

    I'm trying to match 2 tables where one table has just the surname, while the other table has the surname within it, but also a lot of other junk such as titles first names etc.

    I want to match the two tables just using these variables.

    I've tried to match the two tables using a join of the form

    table1.name LIKE '%[table2.name]%'

    But it's not working. I guess because you can't put variables in a matching statement?

    Can this kind of LIKE matching be done in a join?

    Thanks, Bill

  • Instead of

    table1.name LIKE '%[table2.name]%'

    Try

    table1.name LIKE '%' + [table2.name] + '%'

    Or optionally

    table1.name LIKE '%' + RTRIM([table2.name]) + '%'



    Once you understand the BITs, all the pieces come together

  • Thanks again Thomas!

    This is what worked:

    table1.name LIKE '%' + table2.name + '%'

    Cheers, Bill

  • How about using:

    WHERE CHARINDEX(table1.name,table2.name) > 0

    Will that work for you?


    Richard D. Cushing
    Sr. Consultant
    SYNGERGISTIC SOFTWARE SOLUTIONS, LLC

  • Thanks Richard,

    I see that CHARINDEX looks for a string occuring inside another.

    You've put it in the WHERE statement but I can't work out how I would then join my tables?

    Bill

  • SELECT table1.name,table2.name

    FROM table1

    INNER JOIN table2

    ON CHARINDEX(table2.name,table1.name) > 0

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 6 posts - 1 through 5 (of 5 total)

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