Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT WHERE field contains words

Tags:

sql

select

I need a select which would return results like this:

SELECT * FROM MyTable WHERE Column1 CONTAINS 'word1 word2 word3' 

And I need all results, i.e. this includes strings with 'word2 word3 word1' or 'word1 word3 word2' or any other combination of the three.

All words need to be in the result.

like image 264
Mario Avatar asked Jan 12 '13 06:01

Mario


People also ask

How do you check if a word contains in a string in SQL?

Method 1 - Using CHARINDEX() function This function is used to search for a specific word or a substring in an overall string and returns its starting position of match. In case no word is found, then it will return 0 (zero).

How do I select a word in SQL?

To select words with certain values at the end of the word In SQL, we can use pattern matching. A pattern matching allows users to search for certain patterns in the data. It is done using the LIKE operator in SQL. The query uses wildcard characters to match a pattern, Wildcard characters are case-sensitive.


1 Answers

Rather slow, but working method to include any of words:

SELECT * FROM mytable WHERE column1 LIKE '%word1%'    OR column1 LIKE '%word2%'    OR column1 LIKE '%word3%' 

If you need all words to be present, use this:

SELECT * FROM mytable WHERE column1 LIKE '%word1%'   AND column1 LIKE '%word2%'   AND column1 LIKE '%word3%' 

If you want something faster, you need to look into full text search, and this is very specific for each database type.

like image 148
mvp Avatar answered Oct 15 '22 18:10

mvp