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

  • Home
  • SEARCH
  • 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 7072441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:48:33+00:00 2026-05-28T05:48:33+00:00

Given a start and end time, I want to know how many minutes in

  • 0

Given a start and end time, I want to know how many minutes in a given hour are covered.

create function CalcMinsInHour(@start datetime, @end datetime, @hour int)
returns int
as
begin
    --Looking for best way to write this part
end

CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 7)  should return 0
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 8)  should return 45
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 9)  should return 60
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 10) should return 30
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 11) should return 0

Edit: @Start and @End represent employee clock in/out times. So yes they can span two days when they work past midnight, but not more than that.

  • 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-28T05:48:34+00:00Added an answer on May 28, 2026 at 5:48 am

    This should do the trick:

    ALTER FUNCTION dbo.CalcMinsInHour(@start DATETIME, @end DATETIME, @hour INTEGER)
    RETURNS INTEGER 
    AS
    BEGIN
        DECLARE @StartOfHour DATETIME
        DECLARE @EndOfHour DATETIME
        SELECT @StartOfHour = DATEADD(hh, @hour, CAST(CAST(@start AS DATE) AS DATETIME))
        IF NOT (@StartOfHour BETWEEN @start and @end)
            SET @StartOfHour = DATEADD(hh, @hour, CAST(CAST(@end AS DATE) AS DATETIME))
    
        SELECT @EndOfHour = DATEADD(hh, 1, @StartOfHour)
    
    RETURN
    (
    SELECT 
        CASE WHEN @EndOfHour < @start OR @StartOfHour > @end THEN 0 ELSE
            DATEDIFF(mi,
                CASE WHEN @StartOfHour <= @start THEN @start ELSE @StartOfHour END,
                CASE WHEN @EndOfHour > @end THEN @end ELSE @EndOfHour END)  
        END
    )
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given DateTime start = startsomething and DateTime end = endSomething Is there a standard
Given just a std::string iterator, is it possible to determine the start and end
I have a simple java program, and I want to know the time difference
I have an appointments table which saves 2 times. start and end. I want
Given a table like the following: sqlite> CREATE TABLE movies( start_time int ); sqlite>
how do I compute the business days in a week given the parameters: start
Given a table action(start:DATE, length:NUMBER, type:NUMBER), with all records unique, I need to select
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
Is there a good way to start at a given index and move BACKWARDS
I need to determine if a given selection is in between a start line

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.