Sql Case Expression (Nested)

isames
isames used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike EghtebasDatabase and Application Developer
Commented:
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
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial