# Timestamp milliseconds in MySQL

**URL:** <https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647>\
**Category:** Archived Topics\
**Created:** [April 23, 2008, 7:46pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647 "2008-04-23T19:46:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 23, 2008, 7:46pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/1 "2008-04-23T19:46:42Z")

</div>

I’m reading that MySQL does not support fractional seconds in the Timestamp data type. Is this true?

Is there any guidance you can offer to work around this shortcoming to display fractional second data in a chart? Thanks.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [April 23, 2008, 8:07pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/2 "2008-04-23T20:07:07Z")

</div>

Ah yes, I remember clearly the crushing feeling of defeat when I first discovered this shocking shortcoming of MySQL.

Of course, we aren’t alone, see [this google search.](http://www.google.com/search?client=opera&rls=en&q=mysql+millisecond+timestamp+OR+datetime&sourceid=opera&ie=utf-8&oe=utf-8) for lots of information about this issue.

Your best hope for a workaround is to store the date in a DATETIME field, and then have a separate integer column for microseconds, and to select the date, do a

```auto
SELECT DATE_ADD(t_stamp, INTERVAL microsec_val MICROSECOND) FROM ... 
```

Or - switch to SQL Server, Oracle, DB2, etc, etc.

Don’t get me wrong, MySQL is a great database, but this is a really annoying bug.

Good luck,

---

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 23, 2008, 8:30pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/3 "2008-04-23T20:30:56Z")

</div>

So that SELECT statement will produce a DATETIME with fractional seconds, and FPMI can deal with it (just as if it came from a database that supported this). Correct?

Thanks. That should work for me.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [April 23, 2008, 8:33pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/4 "2008-04-23T20:33:53Z")

</div>

I’m pretty sure. You’re going to have to TIAS (try it and see).

---

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 23, 2008, 10:36pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/5 "2008-04-23T22:36:54Z")

</div>

I presume this means I don’t get to use EasyChart, but will construct a dataset to fill an old-style chart. True?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [April 23, 2008, 10:39pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/6 "2008-04-23T22:39:07Z")

</div>

no, DATE\_ADD should return a proper date object that the easy chart can use. You just have to set your x-column values on your pens to the DATE\_ADD() function, rather than the straight T\_STAMP. Or better yet, make a view.

---

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 24, 2008, 3:31am UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/7 "2008-04-24T03:31:00Z")

</div>

All very cool. I got the view working, and the chart working using the view.

The only thing that’s odd about it is this.

There is 30 seconds worth of data at 100 msec intervals - 300 data points. It appears that the x axis resolution is 1 second, so when data is changing (and my pen is plotting points), there is a stack of 10 points every second.

It only gets weirder from here. I created a database view to be able to use the fractional seconds in my timestamp in MySQL. In a MySQL query browser, when I SELECT all the data from the view and ORDER BY the artificial, fractional-second timestamp, the timestamp data is ordered like this.

0.0, 0.9, 0,8, 0.7, … 0.2, 0.1, 1.0, 1.9, 1.8 etc

Apparently that’s also the way the chart orders the data because, when I plot the pen using a line, it connects the wrong ends of the stacks of data, creating a sawtooth effect. I.e. on descending data, the top of the first stack is connected to the bottom of the second stack, etc - sawtooth.

So, some questions:

1. Can the resolution of the x-axis be changed?

2. Can the data be ordered by something other than the timestamp used as the x-axis values. (I have the original unit-second timestamp and the fractional seconds as separate fields in my view, it there’s some way to order by them.)

3. Why is the data being sorted this way in the first place?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [April 24, 2008, 4:01pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/8 "2008-04-24T16:01:21Z")

</div>

1. The x-axis resolution is to the millisecond. Your fractional second data must not be coming through JDBC correctly. ( you could try upgrading the MySQL JDBC driver )

2. No, it always orders by the timestamp.

3. Thats a good question, and the one that you have to fix, although #1 might be a bigger blockage.

There was a reason I signed my first reply to this thread with “Good luck”, now you’re seeing why.

(its not too late to install MS SQl Server Express for your historical data!)

Good luck.

---

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 24, 2008, 5:15pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/9 "2008-04-24T17:15:09Z")

</div>

1. Re the x-axis resolution, is there any way to view the dataset that is being plotted?

2. I have no idea if it would ever be used (except now by me) but it would be nice to have the option of a non-standard ORDER BY clause.

3. I’ll noodle a little with this one.

Sadly, it is indeed too late for a database change. The boss’s demo is within the next hour or so, and he probably wouldn’t appreciate it if something happened and I blew the whole thing up.

Again, thanks for all the good help.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [April 24, 2008, 5:25pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/10 "2008-04-24T17:25:57Z")

</div>

1. nope.

2. I suppose, although I can’t think of any legitimate use other than getting around this limitation.

Heres a thought - since this is for a demo could you just have 1 second chart resolution?

Your only course of action at this point is to use the classic chart, and to manipulate the timestamps in Jython before you feed the data into the chart.

---

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 24, 2008, 6:01pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/11 "2008-04-24T18:01:02Z")

</div>

1. I queried from the VIEW into a table and, indeed, the timestamp doesn’t include fractional seconds. Bummer.

2. No, I can’t think of any other use either, but since I’m sure your programmers are looking for work to do…

As for the demo, he’ll just have to live without this particular screen. No big problem. Thanks again.

---

<div class="post-metadata">

**Author:** ![Robert.McKenzie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/robert.mckenzie/32/4134_2.png) [@Robert.McKenzie](https://forum.inductiveautomation.com/u/Robert.McKenzie)\
**Post date:** [April 24, 2008, 7:05pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/12 "2008-04-24T19:05:18Z")

</div>

bds,

I know this may sounds like a stupid question, but what is your order by clause for the view you’ve created? If my list was in this order,[quote]0.0, 0.9, 0.8, 0.7, … 0.2, 0.1, 1.0, 1.9, 1.8 etc [/quote]I would assume I had`ORDER BY t_stamp ASC, microsec_val DESC` Also, if FactorySQL is logging this data, couldn’t you just order by the index?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [April 24, 2008, 8:01pm UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/13 "2008-04-24T20:01:31Z")

</div>

[quote=“Robert.McKenzie”]bds,

I know this may sounds like a stupid question, but what is your order by clause for the view you’ve created? If my list was in this order,[quote]0.0, 0.9, 0.8, 0.7, … 0.2, 0.1, 1.0, 1.9, 1.8 etc [/quote]I would assume I had`ORDER BY t_stamp ASC, microsec_val DESC` Also, if FactorySQL is logging this data, couldn’t you just order by the index?[/quote]

This data is coming into the easy chart, which forces an order by using the timestamp column.

---

<div class="post-metadata">

**Author:** ![bds](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bds](https://forum.inductiveautomation.com/u/bds)\
**Post date:** [April 25, 2008, 1:37am UTC](https://forum.inductiveautomation.com/t/timestamp-milliseconds-in-mysql/647/14 "2008-04-25T01:37:05Z")

</div>

No DESC in the query. Also note that it is 0.0 then 0.9, etc, not descending order.

Also, the true order is

0.0, 0.9, 0.9 … 0.1, 1.9, 1.8 the descending within the 1.x, then descending within the 2.x, etc.

Mysterious.
