Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

T-SQL: Using CASE Statement in WHERE Clause (Multiple conditions)

$
0
0

I trying to use  a Case Statement in the Where Clause and I'm having difficulties.

The Where Clause has three conditions.  Condition1 AND either Condition2 or Condtion3 must be met.  I use @Variable1 to determine whether Condition2 or Condition3 must be met.  When @Variable1 = "Operations" then Condition2 applies when @Variable1 = "Admin" then Condition3 applies:

  1. Condition1 Field1 = 'Regional'
  2. Condition2 When @Variable1 = "Operations":  Field2 = 'Seattle' AND Field3 = "Primary'
  3. Condition3 When @Variable1 = "Admin":  Field4 = 'Portland'

Thanks for you help.

....bob sutor

My Code:

WHERE

  Field1 = 'Regional'

    AND ((CASE Varible1 WHEN 'Operations' THEN Field2 END = 'Seattle'

               AND CASE Varible1 WHEN 'Operations' THEN Field3 END = 'Primary')

    OR CASE Varible1 WHEN 'Admin' THEN Field4 END = 'Portland')


Bob Sutor


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>