I need to calculate the DateDiff (hours) between two dates, but only during business-hours (8:30 - 16:00, no weekends). This result will then be put into the Reaction_Time column as per the example below.
ID Date Reaction_Time Overdue 1 29.04.2003 15:00:00 1 30.04.2003 11:00:00 3:30 2 30.04.2003 14:00:00 2 01.05.2003 14:00:00 7:30 YES
*Note: I didn't check to see if the dates in example were holidays.
I'm using SQL Server 2005
This will be combined with a bigger query, but for now all I need is this to get started, I'll try to figure out how to put it all together on my own. Thanks for the help!
Edit: Hey, thanks everyone for the replies. But due to the obvious complexity of a solution on SQL side, it was decided we would do this in Excel instead as that's where the report will be moved anyway. Sorry for the trouble, but I really figured it would be simpler than this. As it is, we just don't have the time.
The only catch is that it will give you 3:30 as 3.5 hours but you can fix that easily.
I would recommend building a user defined function that calculates the date difference in business hours according to your rules.
I'm not sure where your
Overdue
value comes from, so I left it off in my example.In a function you can write way more expressive SQL than in a query, and you don't clog your query with business rules, making it hard to maintain.
Also a function can easily be reused. Extending it to include support for holidays (I'm thinking of a
Holidays
table here) would not be too hard. Further refinements are possible without the need to change hard to read nested SELECT/CASE WHEN constructs, which would be the alternative.If I have time today, I'll look into writing an example function.
EDIT: Here is something with bells and whistles, calculating around weekends transparently:
The function returns a
DATETIME
value meant as an offset from date 0 (which is"1900-01-01 00:00:00"
). So for example a timespan of 8:00 hours would be"1900-01-01 08:00:00"
and 25 hours would be"1900-01-02 01:00:00"
. The function result is the time difference in business hours between two dates. No special handling/support for overtime.The function assumes the start of the next available work day (08:30 h) when the
@date1
is off-hours, and the end of the previous available work day (16:00 h) when@date2
is off-hours."next/previous available" means:
@date1
is'2009-02-06 07:00:00'
(Fri), it will become'2009-02-06 08:30:00'
(Fri)@date1
is'2009-02-06 19:00:00'
(Fri), it will become'2009-02-09 08:30:00'
(Mon)@date2
is'2009-02-09 07:00:00'
(Mon), it will become'2009-02-06 16:00:00'
(Fri)@date2
is'2009-02-09 19:00:00'
(Mon), it will become'2009-02-09 16:00:00'
(Mon)Assuming you have a reference-table of the working days (and their hours), then I would use a 3 stage approach (pseudo-sql)
(first preclude the "all in one day" trivial example, since that simplifies the logic)
Obviously it is a bit hard to do properly without more context...
This function will give you the difference in business hours between two given times. This will return the difference in minutes or hours based on the date part parameter.
Use this code : to find out weekend in between dates