Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 147823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:50:57+00:00 2026-05-11T08:50:57+00:00

I need to calculate the DateDiff (hours) between two dates, but only during business-hours

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:50:58+00:00Added an answer on May 11, 2026 at 8:50 am
    DECLARE @BusHourStart DATETIME, @BusHourEnd DATETIME SELECT @BusHourStart = '08:30:00', @BusHourEnd = '16:00:00' DECLARE @BusMinutesStart INT, @BusMinutesEnd INT SELECT @BusMinutesStart = DATEPART(minute,@BusHourStart)+DATEPART(hour,@BusHourStart)*60,  @BusMinutesEnd = DATEPART(minute,@BusHourEnd)+DATEPART(hour,@BusHourEnd)*60  DECLARE @Dates2 TABLE (ID INT, DateStart DATETIME, DateEnd DATETIME) INSERT INTO @Dates2 SELECT 1, '15:00:00 04/29/2003', '11:00:00 04/30/2003' UNION SELECT 2, '14:00:00 04/30/2003', '14:00:00 05/01/2003' UNION SELECT 3, '14:00:00 05/02/2003', '14:00:00 05/06/2003' UNION SELECT 4, '14:00:00 05/02/2003', '14:00:00 05/04/2003' UNION SELECT 5, '07:00:00 05/02/2003', '14:00:00 05/02/2003' UNION SELECT 6, '14:00:00 05/02/2003', '23:00:00 05/02/2003' UNION SELECT 7, '07:00:00 05/02/2003', '08:00:00 05/02/2003' UNION SELECT 8, '22:00:00 05/02/2003', '23:00:00 05/03/2003' UNION SELECT 9, '08:00:00 05/03/2003', '23:00:00 05/04/2003' UNION SELECT 10, '07:00:00 05/02/2003', '23:00:00 05/02/2003'   -- SET DATEFIRST to U.S. English default value of 7. SET DATEFIRST 7  SELECT ID, DateStart, DateEnd, CONVERT(VARCHAR, Minutes/60) +':'+ CONVERT(VARCHAR, Minutes % 60) AS ReactionTime FROM (      SELECT ID, DateStart, DateEnd, Overtime,         CASE              WHEN DayDiff = 0 THEN                  CASE                      WHEN (MinutesEnd - MinutesStart - Overtime) > 0 THEN (MinutesEnd - MinutesStart - Overtime)                      ELSE 0                      END             WHEN DayDiff > 0  THEN                  CASE                      WHEN (StartPart + EndPart - Overtime) > 0 THEN (StartPart + EndPart - Overtime)                      ELSE 0                      END + DayPart             ELSE 0         END AS Minutes      FROM(         SELECT ID, DateStart, DateEnd, DayDiff, MinutesStart, MinutesEnd,                 CASE WHEN(@BusMinutesStart - MinutesStart) > 0 THEN (@BusMinutesStart - MinutesStart) ELSE 0 END +                 CASE WHEN(MinutesEnd - @BusMinutesEnd) > 0 THEN (MinutesEnd - @BusMinutesEnd) ELSE 0 END AS Overtime,                  CASE WHEN(@BusMinutesEnd - MinutesStart) > 0 THEN (@BusMinutesEnd - MinutesStart) ELSE 0 END AS StartPart,                 CASE WHEN(MinutesEnd - @BusMinutesStart) > 0 THEN (MinutesEnd - @BusMinutesStart) ELSE 0 END AS EndPart,                 CASE WHEN DayDiff > 1 THEN (@BusMinutesEnd - @BusMinutesStart)*(DayDiff - 1) ELSE 0 END AS DayPart         FROM (                 SELECT DATEDIFF(d,DateStart, DateEnd) AS DayDiff, ID, DateStart, DateEnd,                   DATEPART(minute,DateStart)+DATEPART(hour,DateStart)*60 AS MinutesStart,                 DATEPART(minute,DateEnd)+DATEPART(hour,DateEnd)*60 AS MinutesEnd                  FROM (                         SELECT ID,                                 CASE                                          WHEN DATEPART(dw, DateStart) = 7                                          THEN DATEADD(SECOND, 1, DATEADD(DAY, DATEDIFF(DAY, 0, DateStart), 2))                                         WHEN DATEPART(dw, DateStart) = 1                                          THEN DATEADD(SECOND, 1, DATEADD(DAY, DATEDIFF(DAY, 0, DateStart), 1))                                 ELSE DateStart END AS DateStart,                                 CASE                                          WHEN DATEPART(dw, DateEnd) = 7                                          THEN DATEADD(SECOND, -1, DATEADD(DAY, DATEDIFF(DAY, 0, DateEnd), 0))                                         WHEN DATEPART(dw, DateEnd) = 1                                          THEN DATEADD(SECOND, -1, DATEADD(DAY, DATEDIFF(DAY, 0, DateEnd), -1))                                 ELSE DateEnd END AS DateEnd FROM @Dates2                 )Weekends         )InMinutes     )Overtime )Calculation 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the year diferences between two dates. Tried using DATEDIFF but
I need to calculate the number of business days between two dates. How can
I need to calculate the difference (in days) between two dates in MySQL excluding
I need to calculate the time passed between two dates. The catch here is
I need to calculate the difference between two timestamps in milliseconds. Unfortunately, the DateDiff-function
I need to calculate the number of complete years between two dates in sql.
DateDiff calculates incorrect: DateDiff(m, 30/06/2011, 24/06/2012) will return 12 But i need to return
I need to calculate the distance (in meters and miles) between two coordinates given
I need to calculate the number of days passed between 2 dates using java.util.Date,
I need to calculate distance between two locations in my android application. Then I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.