Hi all (my first post on the Stack!),
This works:
where
Tran_date between @FromDate and @ToDate
and Range = @Range
and Store_ID =
case when @Range = 'RangeName' then
1234
else
Store_ID
end
but how can I achieve this?:
where
Tran_date between @FromDate and @ToDate
and Range = @Range
and Store_ID
case when @Range = 'RangeName' then
not in (1234, 5678)
else
Store_ID
end
Multiple conditions in CASE statement You can evaluate multiple conditions in the CASE statement.
You can use the OR condition in the WHERE clause to test multiple conditions where the record is returned if any one of the conditions are met. This example uses the WHERE clause to define multiple conditions, but instead of using the AND condition, it uses the OR condition.
IN and NOT IN clause on the same column is legit and logical. It's just like doing Set Minus (Set A - Set B) . thus the result.
CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.
where
Tran_date between @FromDate and @ToDate
and Range = @Range
and Store_ID
case when @Range = 'RangeName' AND Store_Id in (1234, 5678)
9999 -- Assumes 9999 is a non possible value.
-- If it is possible then pick one that isn't.
else
Store_ID
end
I think you want:
AND NOT (@Range = 'RangeName' AND Store_ID IN (1234,5678))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With