Mysql : how to calculate business hrs between two timestamps and neglect weekends

后端 未结 4 1682
南笙
南笙 2021-02-04 06:38

My table has sample data and I need to calculate business hrs between two timestamps in two columns. Business hrs : 9:00am to 5:00pm and neglect Saturday and Sunday, I am not co

4条回答
  •  灰色年华
    2021-02-04 07:14

    try

    SELECT (FLOOR(DATEDIFF (UpdatedDate, CreatedDate) / 7) * 5 + MOD (DATEDIFF (UpdatedDate, CreatedDate), 7)) * 8 +
           TIMEDIFF (TIME (UpdatedDate), TIME(CreatedDate))
    FROM YourTable
    

    The above is not complete as it only "estimates" the weekends... but it should get you started... another option is to work with a table of valid businesshours (for details see this answer from Laurence).

    For reference see MySQL documentation.

提交回复
热议问题