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
 Trailing Months

Author  Topic 

spikyman
Starting Member

6 Posts

Posted - 2013-02-10 : 03:25:14
Hi Guys!
Can anyone resolve this job?

I need to add another date option which would be the prior 12 months up through the last quarter. So if it were April 2013, I would see Mar 2012 to Mar 2013. If it were May 2013, I would see April 2012 to March 2013.
Thank you for your help.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-10 : 06:21:12
[code]
--End of last quarter
DATEADD(qq,DATEDIFF(qq,0,GETDATE()),-1)

-- start of four quarters ago
DATEADD(qq,DATEDIFF(qq,0,GETDATE())-4,0);

-- to use this in a WHERE clause, I would use a slightly different approach
-- as shown in red below:
WHERE
YourDateCol >= DATEADD(qq,DATEDIFF(qq,0,GETDATE())-4,0)
AND YourDateCol < DATEADD(qq,DATEDIFF(qq,0,GETDATE()),0)[/code]
Go to Top of Page

spikyman
Starting Member

6 Posts

Posted - 2013-02-10 : 11:00:58
Thank you very much !
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-10 : 11:27:23
You are very welcome - glad to help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-11 : 00:31:29
quote:
Originally posted by spikyman

Thank you very much !


see logic here

http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -