Comments on: 23:59:59 is not the end of the day. No, really! https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really Blog by Shlomi Noach Wed, 01 Jan 2014 02:47:43 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: cheolhwan.oh https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-227834 Wed, 01 Jan 2014 02:47:43 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-227834 DATEADD(ms,-2, DATEADD(d,DATEDIFF(d,-1,GETDATE()),0) )

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47595 Mon, 01 Aug 2011 03:41:27 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47595 @Jack,

BETWEEN ’2011-07-26 00:00:00′ AND ’2011-07-26 23:59:59′ is correct as long as timestamps are in 1 second resolution. Again: 23:59:59 is the *beginning* of the last second.
When you have better resolution than 1 second (as already exists with MariaDB), this makes the query wrong, since it will *not* catch 23:59:59.01, for example.

Perhaps I should have been more general and talk about “all the different kinds of temporal values, now and in the future”.
Do also note than not many programmers are actually aware of the differences between TIMESTAMP and DATETIME. They choose either; they work out the same queries for both (they look the same, don’t they?).

]]>
By: Jack Y https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47585 Mon, 01 Aug 2011 02:03:35 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47585 MySQL timestamp is 4 bytes so I can’t see why BETWEEN ‘2011-07-26 00:00:00’ AND ‘2011-07-26 23:59:59’ is wrong.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47517 Sun, 31 Jul 2011 05:43:43 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47517 @Kedar,
Fixed, thanks!

]]>
By: Scott David Daniels https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47307 Thu, 28 Jul 2011 19:58:11 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47307 I’d typically do:

SELECT … WHERE timestamp_column BETWEEN ‘2001-07-26’ AND ‘2001-07-07’ AND timestamp_column < '2001-07-27';

As some query optimizers are happier with the BETWEEN construct (treating it as a more restrictive query than a pair of comparisons). This is simply because I try for the most portable queries that are effective.

]]>
By: Kedar https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47271 Thu, 28 Jul 2011 11:16:57 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47271 Well Said!

+ date(timstamp_column)=’2011-07-26′ will work but this won’t catch the indexes.

BTW is it July 26th, 2011 or July 26th, 2001 (check ur query) !! 😉

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47244 Thu, 28 Jul 2011 03:32:56 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47244 @Justin LOL!

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47243 Thu, 28 Jul 2011 03:32:39 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47243 @dror,
adddate() is fine. I tend to favor the use of INTERVAL, because it is such more intuitive for me.

@Baron
Yes, BETWEEN is inclusive, which is why it works correctly for now — for as long precision is 1 second.

@Roland,
Yes, leap seconds, daylight savings etc. are all so nicely managed by time/date math within MySQL, I usually drop all application based calculations and just ask the DB to do the math.

]]>
By: Justin Rovang https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47222 Wed, 27 Jul 2011 22:17:31 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47222 Now your students will use “SELECT * FROM some_table WHERE timstamp_column BETWEEN ‘2001-07-26 00:00:00’ AND ‘2001-07-26 23:59:59.999999’ ”

😉

]]>
By: Roland Bouman https://shlomi-noach.github.io/blog/mysql/235959-is-not-the-end-of-the-day-no-really/comment-page-1#comment-47216 Wed, 27 Jul 2011 20:52:42 +0000 https://shlomi-noach.github.io/blog/?p=3839#comment-47216 Perhaps far-fetched, and probably not applicable to the majority of the MySQL installations (see: http://dev.mysql.com/doc/refman/5.0/en/time-zone-leap-seconds.html) but in the rare case of a leap second, < date + INTERVAL 1 DAY) will always be correct, whereas = ..:59 may be off by one or two seconds.

]]>