How to get all the passed months in the current year.

ex;In the current year if the current month is November want to get all the passed months
(1,2,3,4,5,6,7,8,9,10)  same for Feb to Dec

In the current year if the current month is Jan want to get all the Months from previous year.

Thanks
KavyaVSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
This recursive cte will return you the desired result:
declare @startDate as datetime = '2013-01-01'

;with cte (StartDt, prevMonth, Month)
as (
    select @startDate, dateadd(month,-1,@startDate), datename(month,dateadd(month,-1,@startDate))
    union all
    select dateadd(month,-1,StartDt) , dateadd(month,-1,prevMonth), datename(month,dateadd(month,-2,StartDt))
    from cte
    where dateDiff(month, prevMonth, @startDate) < 12 and datepart(month,prevMonth) > 1
    )
select Month from cte

Open in new window


Result for datetime = '2013-01-01':
|     MONTH |
-------------|--
|  December |
|  November |
|   October |
| September |
|    August |
|      July |
|      June |
|       May |
|     April |
|     March |
|  February |
|   January |

Open in new window


Result for datetime = '2013-09-01':
|    MONTH |
------------|--
|   August |
|     July |
|     June |
|      May |
|    April |
|    March |
| February |
|  January |

Open in new window


SQL Fiddle
0
chaauCommented:
BTW, if you need the month numbers instead of month names, modify the cte like this:
declare @startDate as datetime = '2013-01-01'

;with cte (StartDt, prevMonth, Month)
as (
    select @startDate, dateadd(month,-1,@startDate), datepart(month,dateadd(month,-1,@startDate))
    union all
    select dateadd(month,-1,StartDt) , dateadd(month,-1,prevMonth), datepart(month,dateadd(month,-2,StartDt))
    from cte
    where dateDiff(month, prevMonth, @startDate) < 12 and datepart(month,prevMonth) > 1
    )
select Month from cte

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
No points please.

for the above CTE approaches, if using getdate()
try this as the first line

declare @startDate as datetime = DATEADD(dd, - (DAY(getdate()) - 1), convert(date,getdate()))

(it will force @startDate to be the first of the current month, so all dates calculated will be the first of a month)
0
KavyaVSAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.