January 17, 2012 at 9:53 am
Need some help. If I give it a today's date, it should return the date range of last quarter.
Any idea?
January 17, 2012 at 10:36 am
Is this what you are searching for?
Select dateadd(qq, datediff(qq, 0, @ThisDate), 0)
Whoops that gives you this quarter NOT last quarter ... sorry about that
January 17, 2012 at 10:38 am
Or something like this if he wants the start and end date of the quarter.
SELECT
convert(date, DATEADD(q, DATEDIFF(q,0,GETDATE()),0)) FirstQDate,
convert(date, DATEADD(s,-1,DATEADD(q, DATEDIFF(q,0,GETDATE())+1,0))) LastQDate
Woops, I made the same mistake as bitbucket, lol, this gets you this quarter's range. A -1 in the right spot will get you last quarter though.
Edit 2:
Like this:
SELECT
convert(date, DATEADD(q, DATEDIFF(q,0,GETDATE()) -1 ,0)) FirstQDate,
convert(date, DATEADD(s,-1,DATEADD(q, DATEDIFF(q,0,GETDATE()),0))) LastQDate
Just replace the GETDATE() with whatever date you are looking at.
June 2, 2016 at 12:10 pm
SELECT Dateadd(d,-1,convert(date, DATEADD(q, DATEDIFF(q,0,GETDATE()),0))) LastDateOfLastQuater
June 2, 2016 at 2:53 pm
The following should help:
d e c la r e -- spaced to post from work
@ThisDate date = getdate();
select
@ThisDate,
dateadd(quarter,datediff(quarter,0,@ThisDate),0) CurrentQuarterStart,
dateadd(quarter,datediff(quarter,0,@ThisDate) - 1,0) PreviousQuarterStart,
dateadd(quarter,datediff(quarter,0,@ThisDate),-1) PreviousQuarterEnd;
Personally, I would use >= PreviousQuarterStart and < CurrentQuarterStart for a filter in a WHERE clause.
June 2, 2016 at 6:55 pm
Lynn Pettis (6/2/2016)
The following should help:
d e c la r e -- spaced to post from work
@ThisDate date = getdate();
select
@ThisDate,
dateadd(quarter,datediff(quarter,0,@ThisDate),0) CurrentQuarterStart,
dateadd(quarter,datediff(quarter,0,@ThisDate) - 1,0) PreviousQuarterStart,
dateadd(quarter,datediff(quarter,0,@ThisDate),-1) PreviousQuarterEnd;
Personally, I would use >= PreviousQuarterStart and < CurrentQuarterStart for a filter in a WHERE clause.
+1 for the simplicity and + 1,000,000 for the WHERE clause example to avoid accidental inclusion of whole dates for the next quarter.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply