user3324467
user3324467

Reputation: 19

How to use case Statement in Where Clause in SQLserver

I need my query to check certain conditions based on the Customer's Status

Sample Data:

Table  A
    A_ID Customer_ID  Department  Feild  Case1 Case2 
     1       101           X       A      FX1   Zx1
     2       102           Y       B      FX2   ZX2
     3       103           Z       C      FX3   ZX3
  Table  B ---- 
        B_ID      Customer_ID   Status  Feild  Match_Feild   Case1  Case2 
         1            101         C      Null      A          FX1    ZX1
         2            101       Manual    A        AX         FX0    ZX0

in my case if the status is manual then in the where condition i need to check with Feild alone
else i need to check with other two conditions

where case when Status ='Manual' then a.feild = b.feild else a.Case1 = b.Case1 and a.Case2 = B.case2 end.

I am not sure how to put this in my query

Upvotes: 0

Views: 74

Answers (1)

Shantanu Gupta
Shantanu Gupta

Reputation: 21188

    where (Status = 'Manual' and a.feild = b.feild ) 
          OR 
          (Status<> 'Manual' AND a.Case1 = b.Case1 and a.Case2 = B.case2)

Upvotes: 1

Related Questions