Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get Next Calendar Quarter Date

Author  Topic 

tkepongo
Yak Posting Veteran

62 Posts

Posted - 2011-09-19 : 19:08:23
Is it possible to retrieve the next calendar quarter's first day? For example, I believe the first day of Q4 2011 is October 1st. Is there a SQL statement to retrieve October 1st?

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 2011-09-19 : 19:36:12
Try this:

[CODE]
declare @date date
set @date='2002-01-03'

SELECT DATEADD(QQ, DATEDIFF(QQ, 0, DATEADD(QQ, 1, @DATE)),0)
[/CODE]
Go to Top of Page

Aleph_0
Yak Posting Veteran

79 Posts

Posted - 2011-09-19 : 19:36:19
quote:
Originally posted by tkepongo

Is it possible to retrieve the next calendar quarter's first day? For example, I believe the first day of Q4 2011 is October 1st. Is there a SQL statement to retrieve October 1st?


SELECT DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0)
Go to Top of Page

Aleph_0
Yak Posting Veteran

79 Posts

Posted - 2011-09-19 : 19:38:09
Doh! You beat me to it!
Go to Top of Page

tkepongo
Yak Posting Veteran

62 Posts

Posted - 2011-09-19 : 19:39:44
Thanks, it seems to be working for me!

I have one question though; how would I retrieve the first day of the next quarter if the current quarter is Q4? So if it is 10/1/2011, how can I get 1/1/2012? Will your solutions work?
Go to Top of Page

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 2011-09-19 : 19:41:49
Just change the value of your @date like so:
[CODE]
declare @date date
set @date='2011-10-01'

SELECT DATEADD(QQ, DATEDIFF(QQ, 0, DATEADD(QQ, 1, @DATE)),0) /*Aleph's solution is better*/
DATEADD(QQ, DATEDIFF(QQ, 0, @DATE) +1 ,0) --//Use this
[/CODE]
should return 2012-01-01

And Aleph...that almost NEVER happens :) I usually think about it for about 30 minutes, post, then realize there were 30 posts :p

--//Edit: My original solution took 1 extra step, adding 1 to the datediff of the current quarter to the 1st SQL Server quarter employs one less dateadd function

Go to Top of Page

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 2011-09-19 : 19:43:23
@Aleph...and yours has one less function than mine!
Go to Top of Page

Aleph_0
Yak Posting Veteran

79 Posts

Posted - 2011-09-19 : 19:53:58
quote:
Originally posted by flamblaster
And Aleph...that almost NEVER happens :) I usually think about it for about 30 minutes, post, then realize there were 30 posts :p


Me too, except there are so few questions I can actually answer correctly! Well, at least I think I can answer correctly....

Thanks for the ego boost, though!
Go to Top of Page
   

- Advertisement -