[Solved] Find string according to words count


A) Use String_Split or something similar to create a table for each word.
LG 55 Vacuum theater home

  #table_of_words (text varchar(32))
  text
  LG
  55
  Vacuum
  Theater
  Home

B) join the created table with the
main table using an expression like

SELECT DISTINCT Description 
FROM main_table  Main
JOIN #table_of_words  WD ON Main.Description Like '%'+WD.text+'%'

If you want to only include whole words, you will need to put spaces into the ON clause: (Thanks ZLK)

SELECT DISTINCT Description 
FROM main_table  Main
JOIN #table_of_words  WD ON ' '+Main.Description+' ' 
LIKE '% '+WD.text+' %'

Also note that if you want to deal with commas, you’ll need some additional tweaking.

SELECT DISTINCT Description 
FROM main_table  Main
JOIN #table_of_words  WD ON ' '+Main.Description+' ' 
LIKE '%[, ]'+WD.text+'[, ]%'

Here is how to order the result by number of words found

SELECT description,count(*) as NumberWords
FROM main_table  Main
    JOIN #table_of_words WD ON ' '+Main.description+' ' 
    LIKE '% '+WD.text+' %'
GROUP  BY  description
ORDER BY NumberWords DESC

9

solved Find string according to words count