MySQL week calculation between two dates

前端 未结 2 934
夕颜
夕颜 2020-12-16 12:31

I\'ve been stuck with this issue for days, which is something like calculating the numbers of weeks that lies between two days, for example:

Select @Days = (         


        
相关标签:
2条回答
  • 2020-12-16 12:53
    DATEDIFF(@date1, @date2)/7
    

    That returns a fraction which I'm guessing you'll want to round in some way with CEIL(), ROUND() or FLOOR()

    My test example with two defined dates:

    SELECT FLOOR(DATEDIFF(DATE(20090215), DATE(20090101))/7);
    
    0 讨论(0)
  • 2020-12-16 13:16

    you could also try this as it separates weeks and days.

        SET @day1=DATE('2015-02-02');
        SET @day2=DATE('2015-02-10');
        SELECT CONCAT(SUBSTRING_INDEX(ABS(DATEDIFF(@day1,@day2)/7),'.',1),'Weeks ',
        SUBSTRING_INDEX(ABS(DATEDIFF(@day1,@day2)),'.',1)-SUBSTRING_INDEX(ABS(DATEDIFF(@day1,@day2))/7,'.',1)*7,'Days'
        )AS diff
    
    0 讨论(0)
提交回复
热议问题