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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:44:33+00:00 2026-05-23T17:44:33+00:00

I would like to get datetime column rounded to nearest hour and nearest minute

  • 0

I would like to get datetime column rounded to nearest hour and nearest minute preferably with existing functions.

For this column value 2007-09-22 15:07:38.850, the output will look like:

2007-09-22 15:08 -- nearest minute
2007-09-22 15    -- nearest hour
  • 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-23T17:44:34+00:00Added an answer on May 23, 2026 at 5:44 pm
    declare @dt datetime
    
    set @dt = '09-22-2007 15:07:38.850'
    
    select dateadd(mi, datediff(mi, 0, @dt), 0)
    select dateadd(hour, datediff(hour, 0, @dt), 0)
    

    will return

    2007-09-22 15:07:00.000
    2007-09-22 15:00:00.000
    

    The above just truncates the seconds and minutes, producing the results asked for in the question. As @OMG Ponies pointed out, if you want to round up/down, then you can add half a minute or half an hour respectively, then truncate:

    select dateadd(mi, datediff(mi, 0, dateadd(s, 30, @dt)), 0)
    select dateadd(hour, datediff(hour, 0, dateadd(mi, 30, @dt)), 0)
    

    and you’ll get:

    2007-09-22 15:08:00.000
    2007-09-22 15:00:00.000
    

    Before the date data type was added in SQL Server 2008, I would use the above method to truncate the time portion from a datetime to get only the date. The idea is to determine the number of days between the datetime in question and a fixed point in time (0, which implicitly casts to 1900-01-01 00:00:00.000):

    declare @days int
    set @days = datediff(day, 0, @dt)
    

    and then add that number of days to the fixed point in time, which gives you the original date with the time set to 00:00:00.000:

    select dateadd(day, @days, 0)
    

    or more succinctly:

    select dateadd(day, datediff(day, 0, @dt), 0)
    

    Using a different datepart (e.g. hour, mi) will work accordingly.

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

Sidebar

Related Questions

I have a table with a DATETIME column. I would like to SELECT this
I would like to get a list of the wireless networks available. Ideally this
I have my records in mysql stored with a datetime column. I would like
I have table that has a column of type DateTime, I would like to
I would like to get the sum of a column from database but the
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to get data from from different webpages such as addresses of
I would like to get up-to-date information on Google's index of a website, and
I would like to get the path to the execution directory of a Windows
I would like to get the absolute position of an element in relation to

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.