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
The question says that public holidays should not be considered, so this answer does just that - calculates business hours taking weekends into account, but ignoring possible public holidays.
If you need to take public holidays into account you'd need to have a separate table which would list dates for public holidays, which may differ from year to year and from state to state or country to country. The main formula may stay the same, but you'd need to subtract from its result hours for public holidays that fall within the given range of dates.
Let's create a table with some sample data that covers various cases:
CREATE TABLE T (CreatedDate datetime, UpdatedDate datetime);
INSERT INTO T VALUES
('2012-03-05 09:00:00', '2012-03-05 15:00:00'), -- simple part of the same day
('2012-03-05 10:00:00', '2012-03-06 10:00:00'), -- full day across the midnight
('2012-03-05 11:00:00', '2012-03-06 10:00:00'), -- less than a day across the midnight
('2012-03-05 10:00:00', '2012-03-06 15:00:00'), -- more than a day across the midnight
('2012-03-09 16:00:00', '2012-03-12 10:00:00'), -- over the weekend, less than 7 days
('2012-03-06 16:00:00', '2012-03-15 10:00:00'), -- over the weekend, more than 7 days
('2012-03-09 16:00:00', '2012-03-19 10:00:00'); -- over two weekends
In MS SQL Server I use the following formula:
SELECT
CreatedDate,
UpdatedDate,
DATEDIFF(minute, CreatedDate, UpdatedDate)/60.0 -
DATEDIFF(day, CreatedDate, UpdatedDate)*16 -
DATEDIFF(week, CreatedDate, UpdatedDate)*16 AS BusinessHours
FROM T
Which produces the following result:
+-------------------------+-------------------------+---------------+
| CreatedDate | UpdatedDate | BusinessHours |
+-------------------------+-------------------------+---------------+
| 2012-03-05 09:00:00 | 2012-03-05 15:00:00 | 6 |
| 2012-03-05 10:00:00 | 2012-03-06 10:00:00 | 8 |
| 2012-03-05 11:00:00 | 2012-03-06 10:00:00 | 7 |
| 2012-03-05 10:00:00 | 2012-03-06 15:00:00 | 13 |
| 2012-03-09 16:00:00 | 2012-03-12 10:00:00 | 2 |
| 2012-03-06 16:00:00 | 2012-03-15 10:00:00 | 50 |
| 2012-03-09 16:00:00 | 2012-03-19 10:00:00 | 42 |
+-------------------------+-------------------------+---------------+
It works, because in SQL Server DATEDIFF returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.
Each day has 8 business hours. I calculate total number of hours between two dates, then subtract the number of midnights multiplied by 16 non-business hours per day, then subtract the number of weekends multiplied by 16 (8+8 business hours for Sat+Sun).
It also assumes that the given start and end date/times are during the business hours.
In MySQL the closest equivalent is TIMESTAMPDIFF, but it works differently. It effectively calculates the difference in seconds and divides (discarding the fractional part) by the number of seconds in the chosen unit.
So, to get results that we need we can calculate the TIMESTAMPDIFF between some anchor datetime and CreatedDate and UpdatedDate instead of calculating the difference between CreatedDate and UpdatedDate directly.
I've chosen 2000-01-03 00:00:00, which is a Monday. You can choose any other Monday (or Sunday, if your week starts on Sunday) midnight for the anchor date.
The MySQL query becomes (see SQL Fiddle):
SELECT
CreatedDate,
UpdatedDate,
TIMESTAMPDIFF(MINUTE, CreatedDate, UpdatedDate)/60.0 -
16*(
TIMESTAMPDIFF(DAY, '2000-01-03',UpdatedDate)-
TIMESTAMPDIFF(DAY, '2000-01-03',CreatedDate)
) -
16*(
TIMESTAMPDIFF(WEEK, '2000-01-03',UpdatedDate)-
TIMESTAMPDIFF(WEEK, '2000-01-03',CreatedDate)
) AS BusinessHours
FROM T