How can I calculate business date and time from date assigned in SQL

廉价感情. 提交于 2019-12-11 05:59:14

问题


For example we have date 5/4/2017 01:00:00 PM And we have working hours 08:00 to 17:30 in weekdays How should I calculate SLA Date and Time ?

Date Assigned :5/4/2017 01:00:00 PM

If SLA is 8 Hours from date assigned .

The Expiry Date should be Like : 5/5/2017 11:30:00 AM by excluding weekends and non-business hours.


回答1:


This is a much more complicated problem then I think you are giving it credit for. There are a number of days and times that will fall outside of working hours that you will need to take into account. This will include any holidays, half-days, office closures, etc. This is best dealt with by maintaining a table of dates, hours or even minutes that are considered working time.

If you don't want a table just for this, a simple Dates table coupled with a second table of rules of when a particular date or time is considered working time or not will allow you to derive this.

If you don't even have that, you will need to derive your table each time you want to run your query and include all of your working time rules within this query. The most efficient way to create a table of datetime values is a Tally Table, which in your case can be utilised as follows:

declare @DateAssigned datetime = '05-04-2017 13:00:00';
declare @DayStart time = '08:00:00';
declare @DayEnd time = '17:30:00';
declare @SLAMinutes int = 480; -- 8 hours * 60 minutes per hour


       -- cte to create a table with 10 rows in
with n(n) as (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1)
       -- cte to cross join the rows together, resulting in 10^6 (1 million) rows.  Add or remove joins per number of minutes required.
       -- Use the row_number function to add an incremental number of minutes to the original @DateAssigned to get a table of minutes from the @DateAssigned value.
    ,t(t) as (select dateadd(minute,row_number() over(order by (select null))-1,@DateAssigned) from n n1, n n2, n n3, n n4, n n5, n n6)
       -- Select the first @SLANumber number of rows that meet your Working Time criteria.  We add 2 to this value do resolve the fencepost problem.
    ,d(d) as (select top (@SLAMinutes + 2) t
              from t
              where cast(t as time) >= @DayStart      -- Only return minutes from 08:00 onwards.
                and cast(t as time) <= @DayEnd        -- Only return minutes up to 17:30.
                and datepart(weekday,t) not in (7,1)  -- Only return minutes not on Saturday or Sunday.
              order by t)
  -- The MAX value in the cte d will be the last minute of your SLA window.
select @DateAssigned as DateAssigned
      ,max(d) as SLADeadline
from d;

Which Outputs:

+-------------------------+-------------------------+
|      DateAssigned       |       SLADeadline       |
+-------------------------+-------------------------+
| 2017-05-04 13:00:00.000 | 2017-05-05 11:30:00.000 |
+-------------------------+-------------------------+


来源:https://stackoverflow.com/questions/43776584/how-can-i-calculate-business-date-and-time-from-date-assigned-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!