I have a stored procedure that creates a calendar table that spans many years for my financial reports, example the years run from 01/06/2015 to 31/05/2016. I want to change the weeknumber calculation so that it starts counting the weeks from the 01/06/2016
I found some code that I modified that nearly works but the first week on June is classed as 0 and not 1
DATEDIFF(week
,DATEADD(YEAR
,DATEDIFF(MONTH
,'19000601'
,@StartDate
) / 12
,'19000601'
)
,dateadd(d
,6 - datepart(w
,cast(year(@StartDate)
- case when month(@StartDate) < 6
then 1
else 0
end
as char(4)
)
+'0601'
)
,@StartDate
)
) AS WeekNum,
and
datediff(d
,cast(year(@StartDate) - case when month(@StartDate) < 6
then 1
else 0
end
as char(4)
)
+ '0601'
,@StartDate
) / 7 AS WeekNum,