0

I'm trying to use Not In with a boolean variable in the where to determine whether or not to search in a set for something. For example:

    Select
      BLAH
    FROM
      BLAH
    WHERE
      ...
      AND CASE WHEN @variable = 'false' THEN
           warningcode1 NOT IN (101,102,103)
Freelancer799
  • 15
  • 2
  • 5

3 Answers3

2

Try this

Select
      BLAH
    FROM
      BLAH
    WHERE
      ...
      AND (@variable <> 'false' OR warningcode1 NOT IN (101,102,103))
rs.
  • 26,707
  • 12
  • 68
  • 90
2

Just use:

AND (@variable <> 'false' OR  warningcode1 NOT IN (101,102,103))

So the warningcode1 matters only if the variable = 'false', othwerwise it will be ignored.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

try this with if

AND IF( @variable = 'false' , warningcode1 NOT IN (101,102,103) , warningcode1 IN (101,102,103) )
echo_Me
  • 37,078
  • 5
  • 58
  • 78