I have a Table Valued Parameter (@KEYWORD) which just has one column with 0 to many rows of keywords that will query against one or two database (nvarchar) columns (REMARKS and SUPPLEMENTAL_REMARKS). Super simple concept.
What I am having trouble with is how to write the SQL to have it check for each of the individual keywords supplied against that one (or two) database columns.
Here’s my unfinished part of the WHERE clause…
WHERE
(
CASE WHEN EXISTS (SELECT 1 FROM @KEYWORD) THEN
--check that some combination of either REMARKS or SUPPLEMENTAL_REMARKS contains all supplied values
CASE WHEN (
-- RES.REMARKS (or RES.SUPPLEMENTAL_REMARKS) LIKE %keyword[0]%
-- AND
-- RES.REMARKS (or RES.SUPPLEMENTAL_REMARKS) LIKE %keyword[1]%
-- AND
-- ... (this doesn't work for many reasons, but is just to give an idea)
) THEN
1
ELSE
0
END
ELSE --TVP (@KEYWORD) not supplied, so ignore this filter
1
END
) = 1
Base on the SQL comment below you only want records from res where all the keyword match on either REMARKS or SUPPLEMENTAL_REMARKS.
The easiest way to do that is JOIN and do a count and make sure it matches the number of keywords in @KEYWORD
One way for it into your where is like so
You might want to consider passing @KEYWORDCOUNT in as a parameter since DataTable.Rows.Count almost free
DEMO