September 14, 2010 at 1:39 pm
I'm trying to write out a single CASE statement, but with multiple WHENS in it. I guess I'm writing out the syntax wrong.
******************************************************
This is what I have so far and it works:
CASE
WHEN Code Between 1 and 24 THEN Hours ELSE NULL
END as HoursBillable
******************************************************
I'm wanting to enter another WHEN statement and looks like this, but does not work.
CASE
WHEN Code Between 1 and 24 THEN Hours ELSE
WHEN Code Between 26 and 29 THEN Hours ELSE NULL
END as HoursBillable
******************************************************
Can someone help point out what I'm doing wrong. Thanks!!
September 14, 2010 at 2:07 pm
You can only have one else condition for the case statement, after all of the WHEN conditions.
CASE
WHEN Code Between 1 and 24 THEN Hours
WHEN Code Between 26 and 29 THEN Hours
ELSE NULL
END as HoursBillable
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 14, 2010 at 2:08 pm
you error is really minor; a case can only have ONE "ELSE" statement:
CASE
WHEN Code Between 1 and 24
THEN Hours
WHEN Code Between 26 and 29
THEN Hours
ELSE NULL
END as HoursBillable
Lowell
September 14, 2010 at 2:20 pm
Looks like I got in ahead of Lowell by 1 or 2 ms.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 14, 2010 at 2:24 pm
SWEET!! Thanks! I knew it had to be something minor.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply