Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

like image 467
Parth Bakshi Avatar asked Aug 07 '15 05:08

Parth Bakshi


2 Answers

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+'%')
like image 167
Sateesh Pagolu Avatar answered Oct 03 '22 16:10

Sateesh Pagolu


I had this exact issue with Postgres. Its a bit messy, but I used textcat to add the wildcard since the above '%'+ didn't play well with my query on Metabase

Try this:

SELECT distinct Name, FullName
FROM Table1 a
INNER JOIN Table2 b ON b.FullName LIKE textcat(textcat('%',a.Name),'%')
like image 33
SenatorWaffles Avatar answered Oct 03 '22 17:10

SenatorWaffles