(Just to clarify for future readers: this answer to this old question was added because of a bounty that implied the current answer wasn't satisfying, so I added this solution/definition with additional "configuration options" for all kinds of situations.)
There is no standard definition for the Week of month
, but a general solution would be the following formula, that you can configurate to your needs:
select (dayofmonth(dateField) + 6 + (7 - "min_days_for_partial_week")
- (weekday(datefield) - "weekday_startofweek" + 7) MOD 7) DIV 7 as week_of_month;
where
"weekday_startofweek"
has to be replaced by the weekday that you want to be the first day of the week (0 = Monday
, 6 = Sunday
).
"min_days_for_partial_week"
is the number of days the first week has to have to count as week 1 (values 1
to 7
). Common values will be 1
(the first day of the month is always week 1), 4
(this would be similar to "iso week of the year", where the first week of the year is the week that contains the thursday, so has at least 4 days), and 7
(week 1 is the first complete week).
This formula will return values 0
to 6
. 0
means that the current week is a partial week that doesn't have enough days to count as week 1, and 6
can only happen when you allow partial weeks with less than 3 days to be week 1.
Examples:
If your first day of the week is Monday ("weekday_startofweek" = 0
) and week 1 should always be a whole week ("min_days_for_partial_week" = 7
), this will simplify to
select (dayofmonth(dateField) + 6 - weekday(datefield)) DIV 7 as week_of_month;
E.g., for 2016-06-02
you will get 0
, because June 2016 started with a Wednesday and for 2016-06-06
you will get 1
, since this is the first Monday in June 2016.
To emulate your formula, where the first day of the week is always week 1 ("min_days_for_partial_week" = 1
), and the week starts with Sunday ("weekday_startofweek" = 6
), this will be
select (dayofmonth(dateField) + 12 - (weekday(datefield) + 1) MOD 7) DIV 7 as week_of_month;
Although you might want to comment that properly to know in 2 years where your constants came from.