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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:30:20+00:00 2026-06-02T20:30:20+00:00

I need to write a function that rounds time in one column, called StartTime,

  • 0

I need to write a function that rounds time in one column, called StartTime, to display just the hour and insert it to another column called StartHour. In another column, called EndTime, I also need to use DATEPART to round up to the next incremented hour (ex: 23:33:00.0000000 would be 23 (the hour) + 1 (to round it up to the end hour) = 24 and I would need to save that to the EndHour column).

I want to insert these new values into new columns called StartHour and StartMinute, EndHour, and EndMinute (the StartTime and EndTime columns are the original values I’m working with in time(7) format and for historical purposes, I’m keeping them in their original columns).

Here’s what I have so far in T-SQL:

SELECT (DATEPART(HOUR, [StartTime])) AS StartHour,(DATEPART(MINUTE, [StartTime])) AS StartMinute,
(DATEPART(HOUR, [EndTime])) AS EndHour, 
(DATEPART(MINUTE, [EndTime])) AS EndMinute, StartTime, EndTime
  FROM [test].[dbo].[Outage_Reports]
  ORDER BY OutageDate ASC

Which produces:

StartHour   StartMinute   EndHour       EndMinute      Startime          EndTime
16        0                 17       30            16:00:00    17:30:00

I now need to write this conversion into a stored procedure that also inserts two (or more if the outage goes on for several hours) new rows into the table when the length between start and end is more than one hour. Or just inserts one row when the outage was below or equal to an hour. And then I need to account for that hourly progression in the start and end columns. Like this:

 StartHour   StartMinute    EndHour     EndMinute        Startime        EndTime
      16          0             17            00         16:00:00         17:30:00

(the above reflects the first hour of outage, the second row below reflects the second half hour of the outage until it stopped…both will be tied to the same outage ticket in the table)

    StartHour     StartMinute    EndHour    EndMinute    Startime      EndTime  
   17             30             18          00  16:00:00      17:30:00

The idea is to track website outages hour by hour so they can join to an orders table that tracks orders by hour (and the orders never contain minutes…just hours). So the plan is to make two rows for an outage that goes on for 1.5 hours so the 16, 17 and all the way through the rounded up 18 hourly values can be tied to the orders table with the 30 minute column to act as another point of calculation (so an hour and a half would equal a full hour’s worth of orders plus a half hour’s worth of orders…split out row by row). This way I can better track trends during outages.

I’m a bit at a loss as to how to write this logic to a stored procedure. I would conceivably have to declare the new hour and minute values into variables and for the StartHour column always keep it rounded to the DATEPART hour value (even if it was 16:45…I want to insert 16 into the StartHour column). However, with the EndHour column, I want to insert a rounded up hourly value (17 and then 18 because we went over an hour in that case) into the EndHour column. This would hopefully describe (via these two rows) the close to two hours the outage occurred. And in turn it would allow for an easy join to our orders table

Any guidance on a stored procedure for this logic would be appreciated.

  • 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-02T20:30:21+00:00Added an answer on June 2, 2026 at 8:30 pm

    well, i didn’t really understand the purpose!!
    but the logic may be something like as below—

    declare @StartTime time
    declare @EndTime time
    declare @Temp_StartTime time
    
    declare @temp_StartHour int
    declare @temp_EndHour int
    declare @temp_StartMinute int
    declare @temp_EndMinute int
    
    SET @StartTime='2:30:00'
    SET @EndTime='4:01:00'
    SET @Temp_StartTime=@StartTime
    
    SET @temp_StartHour=DATEPART(HOUR, @StartTime)
    SET @temp_EndHour=DATEPART(HOUR, @EndTime)
    SET @temp_StartMinute=DATEPART(MI, @StartTime)
    SET @temp_EndMinute=DATEPART(MI, @EndTime)
    
    if(@temp_EndMinute>0)
        BEGIN
            SET @temp_EndHour=@temp_EndHour+1
        END
    
    DECLARE @Temp_Table TABLE
    (
      StartHour int,
      StartMinute int,
      EndHour int,
      EndMinute int,
      StartTime time,
      EndTime time
    )
    
    WHile((@temp_EndHour-@temp_StartHour>=1))
        BEGIN
            INSERT INTO @Temp_Table
            SELECT (DATEPART(HOUR, @Temp_StartTime)) AS StartHour,(DATEPART(MINUTE, @Temp_StartTime)) AS StartMinute,
            @temp_StartHour+1 AS EndHour, 
            0 AS EndMinute, @StartTime as StartTime, @EndTime as EndTime
    
            SET @temp_StartHour=@temp_StartHour+1
            SET @Temp_StartTime=DATEADD(HOUR,1,@Temp_StartTime)
    
            if(DATEPART(MI, @Temp_StartTime)!=0)
                BEGIN
                    SET @Temp_StartTime=DATEADD(MI,-@temp_StartMinute,@Temp_StartTime)
                END
        END
    
    SELECT * FROM @Temp_Table  
    

    hope it’ll help.

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

Sidebar

Related Questions

I need to write a function that rounds time in one column to start
I need to write a function that takes 4 bytes as input, performs a
I need to write a function that is passed an instance of a CharField.
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to write a little ruby function that does word wrapping. I have
I need to be able to write a function that shows repeated words from
Basically what I need to do is write a function that takes in a
I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a function that does exactly opposite of preg_quote function. Simply
I need to write a function that can read a file, and add all

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.