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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:45:22+00:00 2026-06-13T21:45:22+00:00

I need to create a query to sum day’s hours and night’s hours from

  • 0

I need to create a query to sum day’s hours and night’s hours from a range intersec.
For example with this range:

8AM-8PM -> day
8PM-8AM -> night

and one or more start-end time period:

7.30AM 10.00PM

I would like to get this values:

12 day's hours
2.5 night's hours

I don’t know which is the bast way to accomplish this function.

  • 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-13T21:45:23+00:00Added an answer on June 13, 2026 at 9:45 pm

    As i wrote in my comment, it is a brain teaser. I hope you can combine this with your code.

    declare @t table(datefrom smalldatetime, dateto smalldatetime)
    insert @t values('2012-01-01 10:30', '2012-01-02 09:00')
    insert @t values('2012-01-01 08:00', '2012-01-02 20:20')
    insert @t values('2012-01-01 19:00', '2012-01-02 13:00')
    insert @t values('2012-01-01 20:00', '2012-01-02 00:00')
    insert @t values('2012-01-01 23:00', '2012-01-03 00:00')
    
    select datefrom, dateto, day, cast(datediff(minute, datefrom, dateto)/60.0 - day as decimal(9,2)) night
    from @t t
    cross apply
    (select dateadd(day, datediff(hour, '1900-01-01 20:00', datefrom)/24, '1900-01-02 08:00') a,
    dateadd(day, datediff(hour, '1901-01-01 09:00', dateto)/24, '1901-01-01 20:00' ) b) c
    cross apply 
    (select cast(((datediff(hour, a, b) - 12 * datediff(day, a,b)) * 60
    +case when a < datefrom then -datediff(minute, a, datefrom) else 0 end
    +case when b > dateto then +datediff(minute, b, dateto) else 0 end) / 60.0 as decimal(9,2)) day) e
    

    Result:

    datefrom          dateto               day    night
    2012-01-01 10:30  2012-01-02 09:00:00  10.50  12.00
    2012-01-01 08:00  2012-01-02 20:20:00  24.00  12.33
    2012-01-01 19:00  2012-01-02 13:00:00   6.00  12.00
    2012-01-01 20:00  2012-01-02 00:00:00   0.00   4.00
    2012-01-01 23:00  2012-01-03 00:00:00  12.00  13.00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to change my query from native-query to ( named-query or create-query )
I need to create a query that will sum the number of True(1) and
I need ideas to optimize this query: SELECT U.Id , U.Name , SUM(P.Credit) AS
Need help to create a query for this. Start | End 2009-01-01 06:00:00 |
I'm used to create query with createQuery() instead of queryBuilder, but I need to
I have a query being created using OLEDB from access and need to get
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I would like to create a view from the following table. This table contains
I have a procedure that gets a sql query in string. Example: SELECT SUM(Refuse)
This query returns the top 25 best sellers from our database, by customer: var

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.