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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:17:18+00:00 2026-06-02T05:17:18+00:00

How can I calculate business hours between two dates? For example we have two

  • 0

How can I calculate business hours between two dates?
For example we have two dates; 01/01/2010 15:00 and 04/01/2010 12:00
And we have working hours 09:00 to 17:00 in weekdays
How can I calculate working hours with sql?

  • 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-06-02T05:17:20+00:00Added an answer on June 2, 2026 at 5:17 am

    Baran’s answer fixed and modified for SQL 2005

    SQL 2008 and above:

    -- =============================================
    -- Author:      Baran Kaynak (modified by Kodak 2012-04-18)
    -- Create date: 14.03.2011
    -- Description: 09:30 ile 17:30 arasındaki iş saatlerini hafta sonlarını almayarak toplar.
    -- =============================================
    CREATE FUNCTION [dbo].[WorkTime] 
    (
        @StartDate DATETIME,
        @FinishDate DATETIME
    )
    RETURNS BIGINT
    AS
    BEGIN
        DECLARE @Temp BIGINT
        SET @Temp=0
    
        DECLARE @FirstDay DATE
        SET @FirstDay = CONVERT(DATE, @StartDate, 112)
    
        DECLARE @LastDay DATE
        SET @LastDay = CONVERT(DATE, @FinishDate, 112)
    
        DECLARE @StartTime TIME
        SET @StartTime = CONVERT(TIME, @StartDate)
    
        DECLARE @FinishTime TIME
        SET @FinishTime = CONVERT(TIME, @FinishDate)
    
        DECLARE @WorkStart TIME
        SET @WorkStart = '09:00'
    
        DECLARE @WorkFinish TIME
        SET @WorkFinish = '17:00'
    
        DECLARE @DailyWorkTime BIGINT
        SET @DailyWorkTime = DATEDIFF(MINUTE, @WorkStart, @WorkFinish)
    
        IF (@StartTime<@WorkStart)
        BEGIN
            SET @StartTime = @WorkStart
        END
        IF (@FinishTime>@WorkFinish)
        BEGIN
            SET @FinishTime=@WorkFinish
        END
        IF (@FinishTime<@WorkStart)
        BEGIN
            SET @FinishTime=@WorkStart
        END
        IF (@StartTime>@WorkFinish)
        BEGIN
            SET @StartTime = @WorkFinish
        END
    
        DECLARE @CurrentDate DATE
        SET @CurrentDate = @FirstDay
        DECLARE @LastDate DATE
        SET @LastDate = @LastDay
    
        WHILE(@CurrentDate<=@LastDate)
        BEGIN       
            IF (DATEPART(dw, @CurrentDate)!=1 AND DATEPART(dw, @CurrentDate)!=7)
            BEGIN
                IF (@CurrentDate!=@FirstDay) AND (@CurrentDate!=@LastDay)
                BEGIN
                    SET @Temp = @Temp + @DailyWorkTime
                END
                --IF it starts at startdate and it finishes not this date find diff between work finish and start as minutes
                ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate!=@LastDay)
                BEGIN
                    SET @Temp = @Temp + DATEDIFF(MINUTE, @StartTime, @WorkFinish)
                END
    
                ELSE IF (@CurrentDate!=@FirstDay) AND (@CurrentDate=@LastDay)
                BEGIN
                    SET @Temp = @Temp + DATEDIFF(MINUTE, @WorkStart, @FinishTime)
                END
                --IF it starts and finishes in the same date
                ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate=@LastDay)
                BEGIN
                    SET @Temp = DATEDIFF(MINUTE, @StartTime, @FinishTime)
                END
            END
            SET @CurrentDate = DATEADD(day, 1, @CurrentDate)
        END
    
        -- Return the result of the function
        IF @Temp<0
        BEGIN
            SET @Temp=0
        END
        RETURN @Temp
    
    END
    

    SQL 2005 and below:

    -- =============================================
    -- Author:      Baran Kaynak (modified by Kodak 2012-04-18)
    -- Create date: 14.03.2011
    -- Description: 09:30 ile 17:30 arasındaki iş saatlerini hafta sonlarını almayarak toplar.
    -- =============================================
    CREATE FUNCTION [dbo].[WorkTime] 
    (
        @StartDate DATETIME,
        @FinishDate DATETIME
    )
    RETURNS BIGINT
    AS
    BEGIN
        DECLARE @Temp BIGINT
        SET @Temp=0
    
        DECLARE @FirstDay DATETIME
        SET @FirstDay = DATEADD(dd, 0, DATEDIFF(dd, 0, @StartDate))
    
        DECLARE @LastDay DATETIME
        SET @LastDay = DATEADD(dd, 0, DATEDIFF(dd, 0, @FinishDate))
    
        DECLARE @StartTime DATETIME
        SET @StartTime = @StartDate - DATEADD(dd, DATEDIFF(dd, 0, @StartDate), 0)
    
        DECLARE @FinishTime DATETIME
        SET @FinishTime = @FinishDate - DATEADD(dd, DATEDIFF(dd, 0, @FinishDate), 0)
    
        DECLARE @WorkStart DATETIME
        SET @WorkStart = CONVERT(DATETIME, '09:00', 8)
    
        DECLARE @WorkFinish DATETIME
        SET @WorkFinish = CONVERT(DATETIME, '17:00', 8)
    
        DECLARE @DailyWorkTime BIGINT
        SET @DailyWorkTime = DATEDIFF(MINUTE, @WorkStart, @WorkFinish)
    
        IF (@StartTime<@WorkStart)
        BEGIN
            SET @StartTime = @WorkStart
        END
        IF (@FinishTime>@WorkFinish)
        BEGIN
            SET @FinishTime=@WorkFinish
        END
        IF (@FinishTime<@WorkStart)
        BEGIN
            SET @FinishTime=@WorkStart
        END
        IF (@StartTime>@WorkFinish)
        BEGIN
            SET @StartTime = @WorkFinish
        END
    
        DECLARE @CurrentDate DATETIME
        SET @CurrentDate = @FirstDay
        DECLARE @LastDate DATETIME
        SET @LastDate = @LastDay
    
        WHILE(@CurrentDate<=@LastDate)
        BEGIN       
            IF (DATEPART(dw, @CurrentDate)!=1 AND DATEPART(dw, @CurrentDate)!=7)
            BEGIN
                IF (@CurrentDate!=@FirstDay) AND (@CurrentDate!=@LastDay)
                BEGIN
                    SET @Temp = @Temp + @DailyWorkTime
                END
                --IF it starts at startdate and it finishes not this date find diff between work finish and start as minutes
                ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate!=@LastDay)
                BEGIN
                    SET @Temp = @Temp + DATEDIFF(MINUTE, @StartTime, @WorkFinish)
                END
    
                ELSE IF (@CurrentDate!=@FirstDay) AND (@CurrentDate=@LastDay)
                BEGIN
                    SET @Temp = @Temp + DATEDIFF(MINUTE, @WorkStart, @FinishTime)
                END
                --IF it starts and finishes in the same date
                ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate=@LastDay)
                BEGIN
                    SET @Temp = DATEDIFF(MINUTE, @StartTime, @FinishTime)
                END
            END
            SET @CurrentDate = DATEADD(day, 1, @CurrentDate)
        END
    
        -- Return the result of the function
        IF @Temp<0
        BEGIN
            SET @Temp=0
        END
        RETURN @Temp
    
    END
    
    • 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 number of business days between two dates. How can
In C#, how can I calculate the number of business (or weekdays) days between
How can I calculate the number of differences between two NSStrings. Example: NSString 1
Can someone explain with an example how we can calculate the time and space
Hi i was wondering if anyone knows how i can calculate the difference between
How I can calculate bit length of an integer in Oracle's PL/SQL? I would
How can I calculate the last business day of the month in .NET?
This is a two-part question: I want to calculate (say) 5 business days ahead
Let's say I have a function which can calculate power of four of a
How can I calculate Next Business Day given a Zend_Date and a cutoff time

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.