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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:25:20+00:00 2026-05-16T11:25:20+00:00

More of a warning than a question: We resolved a very puzzling bug this

  • 0

More of a warning than a question:

We resolved a very puzzling bug this morning. We have a variety of reports that allow users to enter date ranges they want to run. The assumption is, if you ask for a report from 8/1/2010 to 8/10/2010 you meant to include 8/10/2010 so the end-date of the report isn’t 8/10, it’s something after that.

It can’t be 8/11/2010 becuase some of these reports rollup everything that happened during a day grouping them by that day which is at midnight, so a daily rollup would include an extra day – not what we wanted.

To avoid the possibility of missing any items very very close to the end of the day, we computed the end date as ‘one tick’ less than tomorrow:

public static DateTime EndOfDay(DateTime day)
{
    return day.Date.AddDays(1).AddTicks(-1);
}

Internally this ends up something like 8/10/2010 12:59:59.9999PM

Well, when you pass this DateTime to a DATETIME parameter in SQL Server it rounds the value UP to 8/11/2010 00:00:00! And since our query uses

DateField BETWEEN @FromDate AND @ToDate

instead of

DateField >= @FromDate AND DateField < @ToDate

We were seeing reports from 8/1/2010-8/10/2010 include items from 8/11/2010.

The only way we discovered the real problem was by round-tripping the dates thru a string. DateTime.ToString() rounds too so we’d end up with 8/1/2010 12:59:59PM which SQL Server was happy with.

So now our ‘end of day’ method looks like this:

public static DateTime EndOfDay(DateTime day)
{
    // Cant' subtract anything smaller (like a tick) because SQL Server rounds UP! Nice, eh?
    return day.Date.AddDays(1).AddSeconds(-1);
}

Sorry not a question – just thought someone might find it useful.

  • 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-16T11:25:20+00:00Added an answer on May 16, 2026 at 11:25 am

    It’s because of the accuracy of the DATETIME datatype, which has an accuracy (quote):

    Rounded to increments of .000, .003,
    or .007 seconds

    So yes you do have to be careful in certain situations (e.g. 23:59:59.999 will be rounded up to 00:00 of the following day, 23:59:59.998 will be rounded down to 23:59:59.997)

    SELECT CAST('2010-08-27T23:59:59.997' AS DATETIME)
    SELECT CAST('2010-08-27T23:59:59.998' AS DATETIME)
    SELECT CAST('2010-08-27T23:59:59.999' AS DATETIME)
    

    As of SQL Server 2008, there is a new DATETIME2 datatype which gives greater accuracy down to 100 nanoseconds.

    When I’m doing queries on a DATETIME field which contains a time element, I don’t use BETWEEN for this reason.

    e.g. I prefer

    WHERE DateField >= '2010-08-27' AND DateField < '2010-08-28'
    

    instead of:

    WHERE DateField BETWEEN '2010-08-27' AND '2010-08-27T23:59:59.997'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

More than about LINQ to [insert your favorite provider here], this question is about
edit4: wikified, since this seems to have morphed more into a discussion than a
I'm very new to VS2010, so this is more a question about using Visual
More particularly - I have a window handle of another running application. This application
I've searched SO and have been unable to find this question asked before, but
I'm trying to disable warning C0321 ("more than one statement on a single line"
This is more like a design question but related to SQL optimization as well.
More than once I've lost work by accidentally killing a temporary buffer in Emacs.
More out of interest than anything else, but can you compile a DirectX app
More detail to my question: HTML and JavaScript are called client-side code. C# and

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.