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 7633823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:59:36+00:00 2026-05-31T06:59:36+00:00

I need to calculate business hours between to dates. I need to exclude following

  • 0

I need to calculate business hours between to dates.

I need to exclude following from that hours.

  1. Out of working hours (6pm -9am)
  2. Weekends
  3. Holidays

For holidays I have a table HOLIDAYS as

DATE        NAME
----------------------------------
2012-01-02  New Year's Day
2012-04-06  Good Friday
2012-04-09  Easter Monday

Notes:
1. I have hours in start and finish dates (‘2012/03/15 14:00’ to ‘2012/03/22 17:30’).
2. I’m using Sql Server 2005 so I don’t have datetypes DATE or 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. Editorial Team
    Editorial Team
    2026-05-31T06:59:37+00:00Added an answer on May 31, 2026 at 6:59 am

    Try this.

    DECLARE @start_date DATETIME 
    DECLARE @stop_date DATETIME 
    DECLARE @days INT
    
    SELECT @start_date = '2012/03/15 14:00', @stop_date = '2012/03/22 17:30'
    
    SELECT @days = DATEDIFF(DAY, CAST(FLOOR(CAST(@start_date AS FLOAT)) AS DATETIME), CAST(FLOOR(CAST(@stop_date AS FLOAT)) AS DATETIME)) + 1
    
    ;WITH CTE_ALL_DAYS AS (
        SELECT TOP (@days) 
            DATEADD(DAY, rn, CAST(FLOOR(CAST(@start_date AS FLOAT)) AS DATETIME)) AS dt
        FROM (
            SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) - 1 AS rn
            FROM master.sys.all_columns AS a
            CROSS JOIN master.sys.all_columns AS b
            ) AS tally
    )
    SELECT SUM(
            CASE
                WHEN dt = CAST(FLOOR(CAST(@start_date AS FLOAT)) AS DATETIME) THEN 
                    CASE 
                        WHEN DATEDIFF(HOUR, @start_date, DATEADD(HOUR, 18, CAST(FLOOR(CAST(@start_date AS FLOAT)) AS DATETIME))) > 9 THEN 9
                        WHEN DATEDIFF(HOUR, @start_date, DATEADD(HOUR, 18, CAST(FLOOR(CAST(@start_date AS FLOAT)) AS DATETIME))) > 0 THEN DATEDIFF(HOUR, @start_date, DATEADD(HOUR, 18, CAST(FLOOR(CAST(@start_date AS FLOAT)) AS DATETIME))) 
                        ELSE 0 
                    END
                WHEN dt = CAST(FLOOR(CAST(@stop_date AS FLOAT)) AS DATETIME) THEN 
                    CASE 
                        WHEN DATEDIFF(HOUR, DATEADD(HOUR, 9, CAST(FLOOR(CAST(@stop_date AS FLOAT)) AS DATETIME)), @stop_date) > 9 THEN 9
                        WHEN DATEDIFF(HOUR, DATEADD(HOUR, 9, CAST(FLOOR(CAST(@stop_date AS FLOAT)) AS DATETIME)), @stop_date) > 0 THEN DATEDIFF(HOUR, DATEADD(HOUR, 9, CAST(FLOOR(CAST(@stop_date AS FLOAT)) AS DATETIME)), @stop_date) 
                        ELSE 0 
                    END 
                ELSE 9      
            END
        )
    FROM CTE_ALL_DAYS   
    WHERE 
        NOT DATEPART(weekday, dt) IN (1, 7)
        AND NOT dt IN (SELECT [DATE] FROM HOLIDAYS)
    
    • 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 DateDiff (hours) between two dates, but only during business-hours
I need to calculate the number of business days between two dates. How can
i need to calculate the business days between two dates. ex : we have
I need to calculate the within and between run variances from some data as
I need to calculate the number of days passed between 2 dates using java.util.Date,
i need to calculate the number of workdays between two dates. a workday is
I need to calculate the number of seconds that have passed between two events
I need to calculate the variance in minutes or hours from two columns. Using
I need to calculate the time passed between two dates. The catch here is
I need a function in php that will calculate the business week in a

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.