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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:44:04+00:00 2026-05-14T08:44:04+00:00

If I have a date value like 2010-03-01 17:34:12.018 What is the most efficient

  • 0

If I have a date value like 2010-03-01 17:34:12.018

What is the most efficient way to turn this into 2010-03-01 00:00:00.000?

As a secondary question, what is the best way to emulate Oracle’s TRUNC function, which will allow you to truncate at Year, Quarter, Month, Week, Day, Hour, Minute, and Second boundaries?

  • 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-14T08:44:05+00:00Added an answer on May 14, 2026 at 8:44 am

    To round to the nearest whole day, there are three approaches in wide use. The first one uses datediff to find the number of days since the 0 datetime. The 0 datetime corresponds to the 1st of January, 1900. By adding the day difference to the start date, you’ve rounded to a whole day;

    select dateadd(d, 0, datediff(d, 0, getdate()))
    

    The second method is text based: it truncates the text description with varchar(10), leaving only the date part:

    select convert(varchar(10),getdate(),111)
    

    The third method uses the fact that a datetime is really a floating point representing the number of days since 1900. So by rounding it to a whole number, for example using floor, you get the start of the day:

    select cast(floor(cast(getdate() as float)) as datetime)
    

    To answer your second question, the start of the week is trickier. One way is to subtract the day-of-the-week:

    select dateadd(dd, 1 - datepart(dw, getdate()), getdate())
    

    This returns a time part too, so you’d have to combine it with one of the time-stripping methods to get to the first date. For example, with @start_of_day as a variable for readability:

    declare @start_of_day datetime
    set @start_of_day = cast(floor(cast(getdate() as float)) as datetime)
    select dateadd(dd, 1 - datepart(dw, @start_of_day), @start_of_day)
    

    The start of the year, month, hour and minute still work with the “difference since 1900” approach:

    select dateadd(yy, datediff(yy, 0, getdate()), 0)
    select dateadd(m, datediff(m, 0, getdate()), 0)
    select dateadd(hh, datediff(hh, 0, getdate()), 0)
    select dateadd(mi, datediff(mi, 0, getdate()), 0)
    

    Rounding by second requires a different approach, since the number of seconds since 0 gives an overflow. One way around that is using the start of the day, instead of 1900, as a reference date:

    declare @start_of_day datetime
    set @start_of_day = cast(floor(cast(getdate() as float)) as datetime)
    select dateadd(s, datediff(s, @start_of_day, getdate()), @start_of_day)
    

    To round by 5 minutes, adjust the minute rounding method. Take the quotient of the minute difference, for example using /5*5:

    select dateadd(mi, datediff(mi,0,getdate())/5*5, 0)
    

    This works for quarters and half hours as well.

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

Sidebar

Related Questions

I have a table that looks like this, Date Value 01/01/2010 03:59:00 324.44 01/02/2010
In a cell in Excel sheet I have a Date value like: 01/01/2010 14:30:00
I have something like this: [{'date': 1, 'value':5}, {'date':2,'value':3}, ...] and want to map
I have a yaml file which looks like this: --- date: 25-01-2010 version: 0.1
I have a data frame like this: Server Date Server Space 1 2010-01-30 server1
For example, I have this sample data: DATE VALUE ID 2010-09-28 1 20000 2010-09-28
I have a date like 27-12-2010 ( day-month-year ) .I need the following value
I have an input that holds a date value like so 03/15/2012 I am
I have a datetime value in mysql '2010-12-08 16:12:12' that I'd like to get
I have a table1 like this: Date Reading Cost 2009-01-01 5.00 1500.00 2009-01-02 10.00

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.