SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL - Case When w/ more than one condition
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ugh3012
Yak Posting Veteran

62 Posts

Posted - 08/21/2014 :  15:07:27  Show Profile  Reply with Quote

Is it possible to have more than one conditions per line? Example below. It works fine if I do this, "When 'M" then 'Yes' But, I am trying to have it to check for more than one. The below example does not work. What can I do to do something like this.

SELECT
FirstName, LastName,
CASE WorkCode
WHEN 'M', 'E', 'F' THEN 'Yes'
WHEN 'C', 'D' THEN 'No'
END
FROM Employees

gbritton
Aged Yak Warrior

842 Posts

Posted - 08/21/2014 :  15:09:40  Show Profile  Reply with Quote


Posted - 08/21/2014 :  15:07:27  Show Profile  Email Poster  Reply with Quote

Is it possible to have more than one conditions per line? Example below. It works fine if I do this, "When 'M" then 'Yes' But, I am trying to have it to check for more than one. The below example does not work. What can I do to do something like this. 

SELECT
FirstName, LastName,
CASE
WHEN WorkCode in ('M', 'E', 'F') THEN 'Yes'
WHEN WorkCode in ('C', 'D') THEN 'No'
END
FROM Employees
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 08/21/2014 :  15:16:02  Show Profile  Reply with Quote
quote:
Originally posted by gbritton



Posted - 08/21/2014 :  15:07:27  Show Profile  Email Poster  Reply with Quote

Is it possible to have more than one conditions per line? Example below. It works fine if I do this, "When 'M" then 'Yes' But, I am trying to have it to check for more than one. The below example does not work. What can I do to do something like this. 

SELECT
FirstName, LastName,
CASE
WHEN WorkCode in ('M', 'E', 'F') THEN 'Yes'
WHEN WorkCode in ('C', 'D') THEN 'No'
END
FROM Employees




That did not work. I get a red squiggly line under the comma between M and E. The error message is "Incorrect syntax near ','"
Go to Top of Page

gbritton
Aged Yak Warrior

842 Posts

Posted - 08/21/2014 :  15:20:37  Show Profile  Reply with Quote
Works for me.

Here's a working query. I just executed it, then copied and pasted it here:


SELECT
FirstName, LastName,
CASE
WHEN WorkCode in ('M', 'E', 'F') THEN 'Yes'
WHEN WorkCode in ('C', 'D') THEN 'No'
END
FROM (values ('a','b','M')) v(Firstname, Lastname, Workcode)


copy and paste this into ssms. It should work.

PS. you're running this on SQL Server, right? Which version?
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 08/21/2014 :  15:25:33  Show Profile  Reply with Quote
ohhh. I see what you did. You moved the WorkCode down to each when line. My bad. Many thanks.
Go to Top of Page

gbritton
Aged Yak Warrior

842 Posts

Posted - 08/21/2014 :  15:27:10  Show Profile  Reply with Quote
Copy/paste is your friend!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000