Function for week of the month in mysql

后端 未结 4 1242
礼貌的吻别
礼貌的吻别 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 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";
    

提交回复
热议问题