There is a way to do this in SQL, but it is rather complicated. Assuming you can live with a string match up to, say, three fruit names, you do as follows.
Suppose that @fruits is a varchar variable containing a list of fruits to which we add more comma delimiters (in case it contains less than three fruit names):
declare @fruits varchar(80); set @fruits = <list of fruits passed in> + ',_,_,_,';
The following equations are not SQL, but the math behind the substring operations that we will need for like expressions:
NOTE: NOT SQL First fruit word: p1 = charindex(',', @fruits) << position of ',' delimiter v1 = substring(@fruits, 0, p1-1) + '%' << fruit word we seek r1 = substring(@fruits, p1+1) << remainder of string Second fruit word: p2 = charindex(',', r1) v2 = substring(r1, 0, p2-1) + '%' r2 = substring(r1, p2+1) Third fruit word: p3 = charindex(',', r2) v3 = substring(r2, 0, p3-1) + '%' r3 = substring(r2, p3+1) ...and so on...
Now we substitute the first values ββof p1 , v1 and r1 into the second system of equations for p2 , v2 and r2 . In the same way, we replace the second set of values ββwith the third set, etc. We get these monsters for v1 , v2 and v3 :
v1 = substring(@fruits, 0, charindex(',', @fruits)-1) + '%' v2 = substring(substring(@fruits, charindex(',', @fruits)+1), 0, charindex(',', substring(@fruits, charindex(',', @fruits)+1))-1) + '%' v3 = substring(substring(substring(@fruits, charindex(',', @fruits)+1), charindex(',', substring(@fruits, charindex(',', @fruits)+1))+1), 0, charindex(',', substring(substring(@fruits, charindex(',', @fruits)+1), charindex(',', substring(@fruits, charindex(',', @fruits)+1))+1))-1) + '%'
These are the first three like values ββwe need to look for:
select * from fruits where fruit like <v1> or fruit like <v2> or fruit like <v3>
Fully extended query:
select * from fruits where fruit like substring(@fruits, 0, charindex(',', @fruits)-1) + '%' or fruit like substring(substring(@fruits, charindex(',', @fruits)+1), 0, charindex(',', substring(@fruits, charindex(',', @fruits)+1))-1) + '%' or fruit like substring(substring(substring(@fruits, charindex(',', @fruits)+1), charindex(',', substring(@fruits, charindex(',', @fruits)+1))+1), 0, charindex(',', substring(substring(@fruits, charindex(',', @fruits)+1), charindex(',', substring(@fruits, charindex(',', @fruits)+1))+1))-1) + '%'
We can do more work to extract the 4th word, 5th word, 6th word and so on as we like. But each vN value becomes much more complicated than the previous one.
Note. I have not tried this solution, I only mathematically proved it.