Before I give a possible solution to your problem, I must reiterate a couple of points others have already made:
- You can't pass a delimited list in a variable to an IN clause
- What is your data type for the ScoreCount column in your table? Let's assume it's an int, as that would make sense.
So anyway, and assuming you're creating a stored proc, here are a couple of solutions:
- The best solution to your problem is to use a table type variable, as in the answer here: How to pass table value parameters to stored procedure from .net code
See the most upvoted answer not the accepted one.
This is great if you're calling a proc from .Net, however other environments may not support table type parameters, in which case you can do the following:
/* Example values - assume @ScoreCount will ultimately be passed in from caller */
declare @ScoreCount varchar(200)
set @ScoreCount = '2,5,0'
/* */
/* 1. Treat null and empty values as zero */
if IsNull(@ScoreCount, '') = '' SET @ScoreCount = '0'
/* 2. Ensure @ScoreCount starts and ends with comma */
SET @ScoreCount = ',' + @ScoreCount + ','
select * from tblEmployee where @ScoreCount LIKE '%,' + Convert(varchar, ScoreCount) + ',%'
What this does is compare each individual ScoreCount value from the table, to see if it is LIKE the comma separated list. e.g. for each row it will do:
select * from tblEmployee where ',2,5,0,' LIKE '%,2,%' -- True, return this row
select * from tblEmployee where ',2,5,0,' LIKE '%,42,%' -- false, do not return this row
select * from tblEmployee where ',2,5,0,' LIKE '%,0,%' -- True, return this row
... etc for each row.
Note that at comment "2" we prepend and append commas to @ScoreCount to ensure only whole numbers are matched, since we are comparing the numbers as strings.
Note also that is not an efficient query, so it would not be fast for large tables!