Once upon a timestamp(milliseconds)….

Once upon a time`stamp`, in a `data`base far far away, someone filed a bug named: `Microseconds precision is not retained by TIME, DATETIME, and TIMESTAMP field types.` – Bug Number 8523. This was the beginning of 2005, yet now that we are approaching the end of 2009, after 4.5 years, many (including myself) are still asking for this.

In fairness sake, MySQL have indeed supplied a way to retain milli and micro seconds in a decimal field `DECIMAL(17,3)`, and it is also queryable as if it were a timestamp BUT why isn’t it possible to store in a `DATETIME` or `TIMESTAMP` field? Why can’t we run a ’select now()’ or ’select curtime()’ etc and get a full timestamp including milli / micro seconds?

I have counted 37 different usernames asking for this feature, spanning from 15th Feb 2005 to recently. (list found below)

Some have suggested UDFs, others suggested using log4j while others, pretty annoyed, allegedly went to MS SQL and some to Postgres :). The comments were varied but one thing was quite common … a feeling of frustration and or disappointment, probably because despite all the requests for the feature in question to be implemented, it is still missing.

Back in 2007, Kevin Burton also tried to understand why MySQL doesn’t support milliseconds here. Brian Aker gave his view on the matter (back in 2007) in the first comment on the same blog post.

My point is (yeah about time :)) …. this is quite an important feature!! and I’m hoping a few MySQL developers / project managers / community people might read this blog post and maybe give it some more importance.

Now from the technical side, I am going to describing a way, `someone from MySQL has suggested it to me this week` to store milli and micro seconds. Maybe it’ll help you work your milliseconds requirement out (or maybe not). As to the way to generate milli and micro seconds, there is a UDF written by Mr Wadimoff here which you might use.

Storing:

mysql> CREATE TABLE ttable
    -> (
    ->    timecol decimal(17,3)
    -> ) Engine=MyISAM;
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO ttable (timecol) VALUES( 20081223094234.572);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ttable (timecol) VALUES( 20090806133004.132);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ttable (timecol) VALUES( 20100411011258.985);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ttable (timecol) VALUES( 20110411011258.985);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT timestamp(timecol) FROM ttable;
+—————————-+
| timestamp(timecol)         |
+—————————-+
| 2008-12-23 09:42:34.572000 |
| 2009-08-06 13:30:04.132000 |
| 2010-04-11 01:12:58.985000 |
| 2011-04-11 01:12:58.985000 |
+—————————-+
4 rows IN SET (0.00 sec)

mysql> SELECT timecol FROM ttable WHERE timecol < "20090901000000";
+——————–+
| timecol            |
+——————–+
| 20081223094234.572 |
| 20090806133004.132 |
+——————–+
2 rows IN SET (0.00 sec)

mysql> SELECT timecol FROM ttable WHERE timecol > "20090901000000";
+——————–+
| timecol            |
+——————–+
| 20100411011258.985 |
| 20110411011258.985 |
+——————–+
2 rows IN SET (0.00 sec)

mysql> SELECT timecol FROM ttable WHERE timecol > now();
+——————–+
| timecol            |
+——————–+
| 20100411011258.985 |
| 20110411011258.985 |
+——————–+
2 rows IN SET (0.00 sec)

mysql> SELECT datediff(20110411011258.985,20100411011258.985);
+————————————————-+
| datediff(20110411011258.985,20100411011258.985) |
+————————————————-+
|                                             365 |
+————————————————-+
1 row IN SET (0.00 sec)

mysql> SELECT timecol, timestamp(timecol) AS timestamp ,year(timecol) AS year, month(timecol) AS month, day(timecol) AS day,quarter(timecol) AS quarter FROM ttable;
+——————–+—————————-+——+——-+——+———+
| timecol            | timestamp                  | year | month | day  | quarter |
+——————–+—————————-+——+——-+——+———+
| 20081223094234.572 | 2008-12-23 09:42:34.572000 | 2008 |    12 |   23 |       4 |
| 20090806133004.132 | 2009-08-06 13:30:04.132000 | 2009 |     8 |    6 |       3 |
| 20100411011258.985 | 2010-04-11 01:12:58.985000 | 2010 |     4 |   11 |       2 |
| 20110411011258.985 | 2011-04-11 01:12:58.985000 | 2011 |     4 |   11 |       2 |
+——————–+—————————-+——+——-+——+———+
4 rows IN SET (0.00 sec)

Bug link: http://bugs.mysql.com/bug.php?id=8523

List of people asking for this feature:

[15 Feb 2005 21:47] Christopher Miller
[03 May 2005 19:46] Boris Burtin
[30 Jun 2005 10:52] Yoshiaki Tajika
[06 Mar 2006 22:14] Tim Sheehy
[14 Mar 2006 21:50] Oriol Garrote
[17 Aug 2006 22:15] Sami Shalabi
[01 Apr 2007 05:22] Ben Valentine
[02 Apr 2007 23:25] Eric George
[27 Apr 2007 06:10] Verghese Mappillai
[23 May 2007 20:47] Andrew McLaughlin
[25 May 2007 18:24] Stephen Pietrowicz
[31 May 2007 10:51] harjeev chug
[22 Jun 2007 01:17] Peter McCulloch
[20 Jul 2007 16:42] Daren Schwenke
[03 Aug 2007 04:45] Jean-Guy Mossu
[20 Feb 2008 09:32] bodri bodri
[11 Mar 2008 20:12] Nathan Atkinson
[24 Mar 2008 08:35] Sourav Sipani
[02 Jul 2008 05:03] Michael Haselton
[22 Aug 2008 10:32] Aniruddha Shival
[28 Aug 2008 17:37] Jorge Urdaneta
[29 Aug 2008 22:19] Ben Wern
[05 Sep 2008 10:41] Pavel Alexeev
[19 Dec 2008 14:31] Felix Dierich
[09 Feb 2009 07:58] Gabriele Tozzi
[17 Feb 2009 23:20] Paul Craven
[21 Feb 2009 12:55] Thomas Ene
[27 Feb 2009 12:13] Pascal Calovini
[09 Mar 2009 09:03] Siu Ching Pong (Asuka Kenji)
[12 Mar 2009 19:41] Yu Chen
[02 Apr 2009 02:35] Noel Akins
[02 Apr 2009 13:09] Lars Monsees
[19 Apr 2009 17:30] Ryan Shillington
[22 Apr 2009 09:00] Jeff Peff
[04 May 2009 09:30] alastair knowles
[28 May 2009 11:42] Yuri Kirilin
[31 May 2009 04:37] Peter Thairu