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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Last 3 Full Months from today

Author  Topic 

bi89405
Starting Member

35 Posts

Posted - 2010-11-19 : 17:19:09
How can I capture the last 3 full months in sql? For example, if totay is 11/19/2010, I'd like a statement that will return 08/01/2010 and another statement that will return 10/31/2010.

I've seen similar date manipulations done in sql using dateadd, datediff but I am not experienced enough to get the last 3 full months.

Please assist. Thanks!

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-19 : 17:38:43
dateadd(mm,datediff(mm,0,getdate()),0)-1
dateadd(mm,datediff(mm,0,getdate())-3,0)



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2010-11-19 : 18:10:27
I would recommend that you use the first of the current month and less than in your query. Example:

SELECT ...
FROM ...
WHERE somedate >= dateadd(month, datediff(month, 0, getdate()) - 3, 0) -- first of 3 months ago
AND somedate < dateadd(month, datediff(month, 0, getdate()), 0); -- first of this month

Jeff
Go to Top of Page
   

- Advertisement -