Search code examples
sqlsql-serverjoinsql-like

Partial String match SQL - Inner Join


Trying to do a partial string match here but am getting a problem with the LIKE operator. I'm sure its the syntax but I cannot see it

SELECT Name
FROM Table1 a
INNER JOIN Table2 b ON a.Name = b.FullName LIKE '%' + a.Name + '%'

I get an error message when I execute this

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'LIKE'.


Solution

  • Try this

    SELECT distinct Name, FullName
    FROM Table1 a
    INNER JOIN Table2 b ON (b.FullName LIKE '%' + a.Name + '%' OR a.Name like '%'+b.FullName+'%')