January 18, 2010 at 9:07 am
Hello,
I am trying to determine a way to run my report based on dates within the last 4 quarters from the current date. For example... if I am running the report anytime between January & then end of March (Quarter 1), I need the report to return data from Quarters 1-4 of last year. If I am running the report anytime from April 1 - June 30, I need the report to pull Quarters 2,3,4 from last year and Q1 from this year. I need the oldest Quarter to fall off the report each time a new Quarter has started. Any ideas?
Thanks in advance.
January 18, 2010 at 9:10 am
SELECT List, Of, Columns FROM dbo.Whatever WHERE PostingDate Between @StartDate AND @EndDate
Then you can either add the grouping in the reporting engine (they all do this). Or you can group by DATEPART(q, PostingDate)
January 18, 2010 at 9:16 am
Thanks Ninja.
What I am doing is creating a view to pull this info. I need the view to only pull the data on the last 4 quarters... So.. my goal is to have the oldest quarter fall off once a new quarter has started. How can I do that with the Datepart function?
January 18, 2010 at 9:49 am
Prehaps these will get you started (Copied from blog posting by Lynn Pettis)
select dateadd(qq, datediff(qq, 0, @ThisDate), 0) -- Beginning of this quarter (Calendar)
select dateadd(qq, datediff(qq, 0, @ThisDate) + 1, 0) -- Beginning of next quarter (Calendar)
select dateadd(qq, datediff(qq, 0, @ThisDate) - 1, 0) -- Beginning of previous quarter (Calendar)
select dateadd(yy, datediff(yy, 0, @ThisDate), 0) -- Beginning of this year
--Remove time from datetime data type
SELECT dateadd(day,datediff(day, 0,'2009-10-21 14:34:25.543'),0)
Also these articles on SSC:
http://www.sqlservercentral.com/articles/function/67046/
/* Takes in a Fiscal Year and a Date Returns a string representing the year and the period in which
the date falls based on a 4-4-5 accounting method.*/
January 18, 2010 at 10:21 am
Thanks Bit! This line works perfectly from the blog post with a change in a variable.
select dateadd(qq, datediff(qq, 0, @ThisDate) - 1, 0)
Change that to -4 and it works exactly how I need it to.
January 18, 2010 at 4:19 pm
skailey
If those worked for you visit Lynn Pettis blog on SSC and learn a great deal more on some neat datetime handling T-SQL
https://www.sqlservercentral.com/blogs/lynnpettis/archive/2009/03/25/some-common-date-routines.aspx
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply