I want to add an AND condition in my query after the WHERE clause only if I find (a) value(s) from a list of predefined values, otherwise the condition should not be added.
The condition I want to add here is "AND Table2.fieldvalue = importantvalue" only when a parameter value is present in a list of (1001, 1002, 1003, 1004, 1005, 1006, 1007)
Also the parameter that comes in is a STRING with INT values comma separated but I have a user defined function to split and cast it into INT
SELECT field1,field2,field3....
from Table1
join Table2 ON Table1.somefield = Table2.somefield
WHERE Table1.field1 = value
AND Table2.field2 = value
AND ( CASE WHEN @parameter IN ( 1001, 1002, 1003, 1004, 1005, 1006, 1007) AND Table2.fieldvalue = importantvalue THEN 1
ELSE 0
END ) = 1 AND Table2.somethingelse
GROUP BY blah,blah,blah..
HAVING blah,blah