Farnaz Daneshvar
Farnaz Daneshvar

Reputation: 151

How to check if a column contains a substring of string in SQL?

I want to check if a column in my database contains words similar to my sample string. The opposite of

select * from myTable where name like '%words%';

so that if I have record with name=word I could retrieve it. With sample above I can only get result where words is a sub string of name column in myTable, therefore I cant get word

Upvotes: 4

Views: 13895

Answers (1)

JNevill
JNevill

Reputation: 50273

You just flip the two terms in your LIKE operator:

SELECT * 
FROM mytable
WHERE 'words' LIKE CONCAT('%',name,'%')

I believe that LOCATE() and INSTR() may work here too which looks nicer since there isn't a need for concatenating the search term/substring.

SELECT *
FROM mytable
WHERE INSTR('words', name) > 0

Upvotes: 4

Related Questions