Sql Case Expression (Nested)

I am trying to create a worksheet in my Business Intelligence software that is broken down by month. So I created a "calculated column" and added the following sql statement:

Case 
When [vdvVoucher].[PostDate] BETWEEN '2015-01-01T00:00:00' AND '2015-01-31T23:00:00'
then 'January'
Else
Case 
When [vdvVoucher].[PostDate] BETWEEN '2015-02-01T00:00:00' AND '2015-02-28T23:00:00'
then 'February'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-03-01T00:00:00' AND '2015-03-31T23:00:00'
then 'March'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-04-01T00:00:00' AND '2015-04-30T23:00:00'
then 'April'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-05-01T00:00:00' AND '2015-05-31T23:00:00'
then 'May'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-06-01T00:00:00' AND '2015-06-30T23:00:00'
then 'June'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-07-01T00:00:00' AND '2015-07-31T23:00:00'
then 'July'
Else
Case 
When [vdvVoucher].[PostDate] BETWEEN '2015-08-01T00:00:00' AND '2015-08-31T23:00:00'
then 'August'
Else 
Case 
When [vdvVoucher].[PostDate] BETWEEN '2015-09-01T00:00:00' AND '2015-09-30T23:00:00'
then 'September'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-10-01T00:00:00' AND '2015-10-31T23:00:00'
then 'October'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-11-01T00:00:00' AND '2015-11-30T23:00:00'
then 'November'
Else
Case
When [vdvVoucher].[PostDate] BETWEEN '2015-12-01T00:00:00' AND '2015-12-31T23:00:00'
then 'December'
End
END
End
End
End
End
End
End
End
End
End
End

Open in new window


I get the following error: Case expressions may only be nested to level 10.

Please advise.
LVL 1
isamesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
select
case datepart(month, [vdvVoucher].[PostDate])
when 1 then 'Jan'
when 2 then 'Feb'
.
.

when 12 then 'Dec'
End As MonthName
From [vdvVoucher]

Open in new window


Unless you are using this in Order By
0
PortletPaulfreelancerCommented:
There are a couple of issue here.

The first is, you don't need to "nest" those conditions at all.
The way a case expression works is at a new row of data, it considers the first "when", if that fails it goes to the next "when" and if that fails to the next "when" and so on ..... until it gets to an "else" or an "end". (& nb, If a row is covered by a "when" no further "when" conditions are considered)

So you case expression can be re-structured like this:
Case 
When [vdvVoucher].[PostDate] BETWEEN '2015-01-01T00:00:00' AND '2015-01-31T23:00:00' then 'January'
When [vdvVoucher].[PostDate] BETWEEN '2015-02-01T00:00:00' AND '2015-02-28T23:00:00' then 'February'
...
END

Open in new window


Another issue. T23:00:00
That is not the end of a day! it is 1 full hour short of that.
In fact you should NOT USE BETWEEN AT ALL for date ranges.

The very best way to define a date range is like this:

[vdvVoucher].[PostDate] >= '2015010' AND [vdvVoucher].[PostDate] < '2015011'

          notice! less than (the date plus one)

see: "Beware of Between"


And, lastly, why not use DATENAME()

e.g.  
select datename(month,'2015-01-01T00:00:00')

Result:
January

{+edit} or datepart()  which would be more efficient and less language dependent
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.