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.
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)-1dateadd(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. |
 |
|
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 agoAND somedate < dateadd(month, datediff(month, 0, getdate()), 0); -- first of this monthJeff |
 |
|
|
|
|
|
|