Function for week of the month in mysql

后端 未结 4 1146
礼貌的吻别
礼貌的吻别 2020-12-03 15:26

I was looking for a simple function to get the week of the month (rather than the easy week of the year) in a mysql query.

The best I could come up with was:

相关标签:
4条回答
  • 2020-12-03 15:55

    There is an alternative that's sometimes used in reporting databases. It's to create a table, let's call it ALMANAC, that has one row per date (the key), and has every needed attribute of the date that might be useful for reporting purposes.

    In addition to the week of the month column, there could be a column for whether or not the date is a company holiday, and things like that. If your company had a fiscal year that starts in July or some other month, you could include the fiscal year, fiscal month, fiscal week, etc. that each date belongs to.

    Then you write one program to populate this table out of thin air, given a range of dates to populate. You include all the crazy calendar calculations just once in this program.

    Then, when you need to know the attribute for a date in some other table, you just do a join, and use the column. Yes, it's one more join. And no, this table isn't normalized. But it's still good design for certain very specific needs.

    0 讨论(0)
  • 2020-12-03 16:09

    AFAIK, there is no standard on the first week of month.

    First week of year is the week containing Jan 4th.

    How do you define first week of month?

    UPDATE:

    You'll need to rewrite your query like this:

    SELECT  WEEK(dateField, 5) -
            WEEK(DATE_SUB(dateField, INTERVAL DAYOFMONTH(dateField) - 1 DAY), 5) + 1
    

    so that the year transitions are handled correctly, and the weeks start on Monday.

    Otherwise, your query is fine.

    0 讨论(0)
  • 2020-12-03 16:09

    (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.

    0 讨论(0)
  • 2020-12-03 16:12

    My solution with a week starts on a Sunday.

    SELECT    ( 1 + ((DATE_FORMAT( DATE_ADD(LAST_DAY( DATE_ADD('2014-07-17',
        INTERVAL -1 MONTH)), INTERVAL 1 DAY),'%w')+1) + 
        (DATE_FORMAT('2014-07-17', '%d')-2) ) DIV 7) "week_of_month";
    
    0 讨论(0)
提交回复
热议问题