trying to do a small stored procedure without needing to add freetext indexing just for this (SQL Server 2008)
Basically, I want to find all records where a certain field contains all the words from a parameter.
So if in the field I have “This is a test field”, and the parameter to my SP would be “this test field” it would return it, as it would if the parameter was “field this test”.
The table is very small (4000) record and load will be low, so efficiency is not a big deal. Right now the only solution i can think of is to split both strings with table valued function and go from there.
Any simpler idea?
Thanks!
Here is a solution using recursive CTEs. This actually uses two separate recursions. The first one splits the strings into tokens and the second one recursively filters the records using each token.
This took me about 15 seconds to search a table of 10k records for the search string ‘this is a test field’.. (the more words in the string, the longer it takes.. )
Edit
If you want a fuzzy search i.e return closely matching results even if there wasnt an exact match, you could modify the last line in the query to be –
select * from (select max(ind) as ind, myfield from filter group by myfield) t order by ind desc‘ind’ would give you the number of words from the search string found in myfield.