Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
How to I create a result set that returns all months from the past 12 months from the last day of the last month?2012-042012-032012-02...2011-05Thanks for your help.
Skorch
Constraint Violating Yak Guru
300 Posts
Posted - 2012-05-24 : 14:11:13
This should do it..
declare @date datetimeset @date = dateadd(mm,datediff(mm,0,getdate()),-1) --last date of last monthselect convert(char(7), dateadd(mm, -number, @date), 20)from (select top 12 number from master.dbo.spt_values where name is null ) t
Some days you're the dog, and some days you're the fire hydrant.