Link to home
Start Free TrialLog in
Avatar of KavyaVS
KavyaVS

asked on

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
SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
Avatar of KavyaVS
KavyaVS

ASKER

Thanks