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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:06:52+00:00 2026-05-12T05:06:52+00:00

I have two Datetime fields that I wish to add together. They are in

  • 0

I have two Datetime fields that I wish to add together. They are in the following format: ’01/01/1900 00:00:00′.

The main issue with this is that I want the calculation to only include working hours.
The working day is between 08:30 and 17:30 and does not include weekends:

Also if the first field starts out of the working day or is on a weekend then the second field should be added from the start of the next working day.

For example:

`’26/06/2009 15:45:00′ + ’01/01/1900 09:00:00′ = ’29/06/1900 15:45:00′

’12/07/2009 14:22:36′ + ’01/01/1900 18:00:00′ = ’13/07/1900 08:30:00′

’15/07/2009 08:50:00′ + ’01/01/1900 04:00:00′ = ’15/07/2009 12:50:00’`

Im pretty sure that this is going to involve creating a user defined function to work this out but I have no idea how to even start this(I am quite out of my depth here) Could anyone offer me some advice on how to achieve this?

  • 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-12T05:06:52+00:00Added an answer on May 12, 2026 at 5:06 am

    try this, you might have to put it in a function

    DECLARE @Date DATETIME,
            @StartOfDay FLOAT,
            @EndOfDay FLOAT,
            @DateAdd DATETIME
    
    SELECT  @Date ='2009-06-26 15:45:00.000',
            @StartOfDay = 8.5,
            @EndOfDay = 17.5,
            @DateAdd = '1900-01-01 09:00:00.000'
    
    --fix up start date
    --before start of day, move to start of day
    IF ((CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24) < @StartOfDay)
    BEGIN
        SET @Date = DATEADD(mi, @StartOfDay * 60, DATEDIFF(dd,0,@Date))
    END
    
    --after close of day, move to start of next day
    IF ((CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24) > @EndOfDay)
    BEGIN
        SET @Date = DATEADD(mi, @StartOfDay * 60, DATEDIFF(dd,0,@Date)) + 1
    END
    
    --move to monday if on weekend
    WHILE DATENAME(dw, @Date) IN ('Saturday','Sunday')
    BEGIN
        SET @Date = @Date + 1
    END
    
    --get the number of hours to add and the total hours per day
    DECLARE @HoursPerDay FLOAT
    DECLARE @HoursAdd FLOAT
    SET @HoursAdd = DATEDIFF(hh, '1900-01-01 00:00:00.000', @DateAdd)
    SET @HoursPerDay = @EndOfDay - @StartOfDay
    
    --date the time of geiven day
    DECLARE @CurrentHours FLOAT
    SET @CurrentHours = CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24
    
    --if we stay in the same day, all is fine
    IF (@CurrentHours + @HoursAdd <= @EndOfDay)
    BEGIN
        SET @Date = @Date + @DateAdd
    END
    ELSE
    BEGIN
        --remove part of day
        SET @HoursAdd = @HoursAdd - (@EndOfDay - @CurrentHours)
        --,ove to next day
        SET @Date = DATEADD(dd,0, DATEDIFF(dd,0,@Date)) + 1
    
        --loop day
        WHILE @HoursAdd > 0
        BEGIN
            --add day but keep hours to add same
            IF (DATENAME(dw,@Date) IN ('Saturday','Sunday'))
            BEGIN
                SET @Date = @Date + 1
            END
            ELSE
            BEGIN
                --add a day, and reduce hours to add
                IF (@HoursAdd > @HoursPerDay)
                BEGIN
                    SET @Date = @Date + 1
                    SET @HoursAdd = @HoursAdd - @HoursPerDay
                END
                ELSE
                BEGIN
                    --add the remainder of the day
                    SET @Date = DATEADD(mi, (@HoursAdd + @StartOfDay) * 60, DATEDIFF(dd,0,@Date))
                    SET @HoursAdd = 0
                END
            END     
        END
    END
    
    SELECT @Date
    

    Hope that helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a table that (among other columns) has two DATETIME columns, how
I have two DateTime objects: StartDate and EndDate . I want to make sure
I have two applications written in Java that communicate with each other using XML
I have two tables that holds inforamtion for SKU. Log table has datetimefield 2008-10-26
I take the difference between two DateTime fields, and store it in a TimeSpan
I have two tables: articles and articletags articles: id, author, date_time, article_text articletags: id,
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two identical tables and need to copy rows from table to another.

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.