I had a similar problem: I needed to calculate the week numbers based on the following rules:
- Week Starts Friday
- The remaining days of the year (all days after the last Friday of the year that do not end in the week) should be considered in the first week of the next year.
For example:
- 12/27/2012 (Thursday) should be week 52 of 2012
- 12/28/2012 (Friday) should be 1 week of 2013.
- Week 1 of 2013 is from 12/28/2012 to 3/1/2013
I made this expression that calculates both YEAR and WEEKNUMBER based on these rules, which you can easily adapt to your event:
SELECT IF(ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7) > 52, YEAR(current_date)+1, YEAR(current_date)), IF(ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7) > 52, 1, ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7));
The hard part is just an expression:
ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7)
The rest (If clauses) are intended only to adapt the result of the expression to make year + 1 and week = 1 at week 53.
I will try to explain this expression as best as possible. The following expression gives you the week number purely simple (the day of the year divided by 7 days a week is rounded up):
ceil(( dayofyear(current_date))/7)
But now you want to start it on Friday (or any other day). To do this, you need to add to the current day, the days of the first week that were part of the previous year (it looks like your current one really started a few days ago, because your first week contains days from the previous year). This expression calculates this offset depending on the day of the week in January / 1:
dayofweek(date_format(current_date, '%Y-01-01'))+OFFSET
The offset is the difference between 7 and the weekday number you want to start the week:
- 0 for saturday
- 1 on friday
- 2 for thursday
- 3 for Wednesday ...
So, now you just need to add it to the previous one, resulting in the above expression, which calculates the numbers of the weeks starting on any day of the week and assuming that week 1 starts in the previous year:
ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+OFFSET )/7)
Then I just added IF, which turns week 53 into week 1, and the other - add 1 to the year if in week 53.