SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trailing Months
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

spikyman
Starting Member

Slovakia
6 Posts

Posted - 02/10/2013 :  03:25:14  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 02/10/2013 :  06:21:12  Show Profile  Reply with Quote

--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)

Go to Top of Page

spikyman
Starting Member

Slovakia
6 Posts

Posted - 02/10/2013 :  11:00:58  Show Profile  Reply with Quote
Thank you very much !
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 02/10/2013 :  11:27:23  Show Profile  Reply with Quote
You are very welcome - glad to help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/11/2013 :  00:31:29  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000