views:

1007

answers:

3

What's the most efficient way to calculate the last day of the prior quarter?

Example: given the date 11/19/2008, I want to return 9/30/2008.

Platform is SQL Server

+1  A: 

I came up with this (tested for all months):

select dateadd(dd,-1,dateadd(qq,datediff(qq,0,'11/19/2008'),0)),
       dateadd(dd,-1,dateadd(qq,datediff(qq,0,'10/19/2008'),0)),
       dateadd(dd,-1,dateadd(qq,datediff(qq,0,'12/19/2008'),0))

It might turn out to be the simplest.

TrickyNixon
+4  A: 

If @Date has the date in question

Select DateAdd(day, -1, dateadd(qq, DateDiff(qq, 0, @Date), 0))
Charles Bretana
I was thinking along the same lines, but I didn't know about the "qq" bits. Makes things a lot easier...
TToni
+2  A: 

actually simpler is: SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), -1)

StrEagle