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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:17:37+00:00 2026-06-07T00:17:37+00:00

Using SQL Server 2008 R2 we are looking for a way to select the

  • 0

Using SQL Server 2008 R2 we are looking for a way to select the shift hours that an employee has that are during the night which in the this case 22.00 and 6.00 +1.

Our problem becomes how to get the hours when the shift crosses midnight or how we get the overlap when a shift begins 05.30 to 22.30 and has an overlap in both the beginning and end of the shift.

Here is an example, theses are the data available in the database and the result we are looking for:

        startDateTime     |       endDateTime         |   nightHours
--------------------------+---------------------------+----------------
 2012-07-04 05:00:00.000    2012-07-04 23:00:00.000          2 
 2012-07-04 18:00:00.000    2012-07-05 05:00:00.000          7

Does anyone have an example or a few good pointer that we can use.

  • 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-07T00:17:38+00:00Added an answer on June 7, 2026 at 12:17 am

    This may be overly complex, but it does work. We use a number of CTEs to construct useful intermediate representations:

    declare @Times table (
        ID int not null,
        StartTime datetime not null,
        EndTime datetime not null
    )
    insert into @Times (ID,StartTime,EndTime)
    select 1,'2012-07-04T05:00:00.000','2012-07-04T23:00:00.000' union all
    select 2,'2012-07-04T18:00:00.000','2012-07-05T05:00:00.000'
    
    ;With Start as (
        select MIN(DATEADD(day,DATEDIFF(day,0,StartTime),0)) as StartDay from @Times
    ), Ends as (
        select MAX(EndTime) EndTime from @Times
    ), Nights as (
        select DATEADD(hour,-2,StartDay) as NightStart,DATEADD(hour,6,StartDay) as NightEnd from Start
        union all
        select DATEADD(DAY,1,NightStart),DATEADD(DAY,1,NightEnd) from Nights n
        inner join Ends e on n.NightStart < e.EndTime
    ), Overlaps as (
        select
            t.ID,
            CASE WHEN n.NightStart > t.StartTime THEN n.NightStart ELSE t.StartTime END as StartPeriod,
            CASE WHEN n.NightEnd < t.EndTime THEN n.NightEnd ELSE t.EndTime END as EndPeriod
        from
            @Times t
                inner join
            Nights n
                on
                    t.EndTime > n.NightStart and
                    t.StartTime < n.NightEnd
    ), Totals as (
        select ID,SUM(DATEDIFF(hour,StartPeriod,EndPeriod)) as TotalHours
        from Overlaps
        group by ID
    )
    select
        *
    from
        @Times t
            inner join
        Totals tot
            on
                t.ID = tot.ID
    

    Result:

    ID          StartTime               EndTime                 ID          TotalHours
    ----------- ----------------------- ----------------------- ----------- -----------
    1           2012-07-04 05:00:00.000 2012-07-04 23:00:00.000 1           2
    2           2012-07-04 18:00:00.000 2012-07-05 05:00:00.000 2           7
    

    You’ll note that I had to add an ID column in order to get my correlation to work.

    The Start CTE finds the earliest applicable midnight. The End CTE finds the last time for which we need to find overlapping nights. Then, the recursive Nights CTE computes every night between those two points in time. We then join this back to the original table (in Overlaps) to find those periods in each night which apply. Finally, in Totals, we compute how many hours each overlapping period contributed.

    This should work for multi-day events. You might want to change the Totals CTE to use minutes, or apply some other rounding functions, if you need to count partial hours.

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

Sidebar

Related Questions

I'm using SQL Server 2008. I'm looking for a creative way to save and
Using SQL Server 2008. I have a stored proc which has start and end
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
Using SQL Server 2008, is there a way to allow inserts to a table
I'm looking for a way to create a backup of a SQL Server 2008
I'm using sql server 2005/2008 and I have a table that it's PK (rec_index)
using : SQL Server 2008 R2, Entityframework 4.3.1 my scenario : I read my
I am using SQL Server 2008 R2, I have a script to update the
I am using SQL Server 2008 R2 and my stored procedure takes in a
I'm using SQL Server 2008. I have a table with x amount of rows.

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.