SQL to return the number of working days between 2 passed in dates

前端 未结 6 1561
礼貌的吻别
礼貌的吻别 2021-01-07 08:10

I need to write an sql query that returns the number of Working days (Monday - Friday) between two given dates.

I was wondering what would be the most efficient way

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-07 08:52

    an easy way to calculate to number of weekdays between 2 dates is :

    SELECT
    date1,
    date2,
    ((date2-date1)-2*FLOOR((date2-date1)/7)-DECODE(SIGN(TO_CHAR(date2,'D')-
        TO_CHAR(date1,'D')),-1,2,0)+DECODE(TO_CHAR(date1,'D'),7,1,0)-
        DECODE(TO_CHAR(date2,'D'),7,1,0))*24 as WorkDays
    FROM
      tablename
    ORDER BY date1,date2
    

提交回复
热议问题