[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Calculate elapsed business hours - SQL 2008

Posted on 2010-11-24
5
Medium Priority
?
962 Views
Last Modified: 2012-05-10
Our business is open 9-5, Monday to Friday.  I am using the SQL function in the attached text file to calculate the elapsed business hours between two datetimes.  The variables are as follows:
@startdate - the start datetime of the period (datetime)
@enddate - the end date of the period (datetime)
@starttime - the start of the business day (vchar(9))
@endtime - the end of the business day (vchar(9))

The function is called with a SQL statement that passes the values of these variables to the function.

Does anyone have any idea how I would go about modifying this function to exclude Saturdays and Sundays from the calculation?

Thanks!
business-hours.txt
0
Comment
Question by:I_play_with_DNA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 17

Expert Comment

by:OriNetworks
ID: 34208889
FIrst I would ask why the times are separated from the dates.

Create a new combined date/time
e.g.
@startdate = '1/1/2010'
@enddate = ' 1/2/2010'
@starttime = '9:00 am'
@endtime = '12:30 pm'

@newstartdate = '1/1/2010 9:00:00 am'
@newenddate = '1/2/2010 12:30:00 pm'

select datediff(minute,@newstartdate,@newenddate)/60 = the amount of hours spent
0
 
LVL 17

Expert Comment

by:OriNetworks
ID: 34208928
sorry i shouldnt have thought it was that easy.

Add to that

select @minutesUsed=datediff(minute,@newstartdate,@newenddate)

Select @minutesUsed=(@minutesUsed-(case when @StartDate<>@EndDate then datediff(day,@startdate,@enddate)*900))

this will remove the after hours minutes between days. You can also do a similar function to check for weekends that have passed.
0
 
LVL 7

Accepted Solution

by:
rmm2001 earned 668 total points
ID: 34208994
Put in a where DATEPART(weekday, @startdate) NOT IN (1, 7) (1 is sunday...7 is saturday)
0
 
LVL 7

Assisted Solution

by:Cboudroz
Cboudroz earned 668 total points
ID: 34209882
add this in the where clause:

and DATEPART(dw,DateValue) not in (1,7)
0
 
LVL 17

Assisted Solution

by:OriNetworks
OriNetworks earned 664 total points
ID: 34210702
Ithe datelart cannot be and otherwise it will evaluate as false for start or end date on weekends and not return a result. You would have to add a case statement with the datepart to subtract those days but this is not good either because it would not calculate weekends in between so oh have to use it in conjunction with a datediff to find out how many weekends are between those dates.
,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question