SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Calculating the Number of Business Hours Passed Since a Point of Time

By Mazharuddin Ehsan, 2008/01/22

Total article views: 595 | Views in the last 30 days: 21

Calculating the number of hours passed since a point of time

This set of user defined functions is used to calculate the number of hours passed since a given date and time excluding the non-working hours . The set contains 8 functions. The function which is called to do the job is [CalcTimeSinceCreated]. This function takes a datetime type value as input. All the other 7 functions are used internally.

This code assumes the working schedule as follows:

  • 8 working hours per day from 7.30 AM to 4.00 PM
  • Lunch period from 11.30 AM to 12 Noon.
  • Saturday & Sunday as off days

The code can be modified to suit any other working hours and days pattern. I used this code to calculate the actual number of working hours passed since a job was started. It can be modified to calculate the number of working hours taken to complete tasks (The function CalcTimeSinceCreated will have to be modified to take two parameters in that case as 'task beginning date&time' and 'task ending date&time', instead of one at present).


Suppose the time right now is November 20, 2 PM and we want to calculate the number of working hours passed since November 16, 3:30 PM. The function will give the result 14.5 hours.

select dbo.CalcTimeSinceCreated('2007-11-16 15:30')











Result = 14.5

 

The calculation goes as follows:

Date Day Hours Explanation
November 16 Friday 0.5 hours The task started at 3.30 PM and the working hour is upto 4.00 PM
November 17 Saturday 0 hours Saturday is an off day
November 18 Sunday 0 hours Sunday is a off day
November 19 Monday 8 hours 8 full hours in the day
November 20 Tuesday 6 hours From 7.30 AM to 2 PM excuding half an hour for the lunch time
Total Hours: 14.5  


By Mazharuddin Ehsan, 2008/01/22

Total article views: 595 | Views in the last 30 days: 21
Your response
 
 
Related tags
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com