Multiple Criteria In Case Statement [duplicate]

2019-09-17 06:27发布

问题:

This question already has an answer here:

  • How do I do multiple CASE WHEN conditions using SQL Server 2008? 8 answers

I know you can do a 1 to 1 relationship in a case statement like such

Select case userID when '12345' Then '12' Else userID End from userInformation

and I don't think you can add 2 criteria in here, but is it possible (if not how could i add a 2nd criteria) to say

Select case userID when '12345' And status is 'Active' Then '12' Else userID End from userInformation

回答1:

Use a slightly different syntax:

 Select case WHEN userID = '12345' And status = 'Active' Then '12' Else userID End 
 from userInformation