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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With