Node-red to mysql - incorrect datetime value

Hello there, I am new to Node-Red but I like the idea and possiblilities very much. With my very first project, I tried to implement the "Log MQTT to MySQL" flow (Log MQTT to MySQL (flow) - Node-RED)

But I haven´t get it to work so far, I keep getting this error code :slight_smile:
"Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2020-02-06T09:55:33.184Z' for column 'timestamp' at row 1"

I have been looking all over the place to find a solution, it seems, that a lot of people have the same problem, but the simple answer, how to change the ISO-timestamp to something, that MySQL accepts, did not show up (or did I simply not find).

Which function can I use instead of

out = out + "VALUES ('" + new Date().toISOString() + "','"

I appreciate any help,
Jürgen

Please search the forum, it has been answered here many times previously...

Hi Paul, thank you for your reply, I was hoping for such an answer. Have a great day !

Try searching for;
ER_TRUNCATED_WRONG_VALUE

1 Like

Hi Paul, I found what I was looking for, but not in the forum! Thanks again!

1 Like

Feel free to add a link here - ready for next time .

2 Likes

Hi Jay (I guess, that is your name),

I found a table, where the javascript date/time methods are described, I then changed the method from ISOString to toLocaleDateString(), but now, my table shows only the date and not the time. What I find confusing, is, that I followed the instructions on how to insert MQTT data into MySQL step by step, set up the database accordingly and still get error codes (apparently not only me).

Did you look in this forum?

The details for how to use date/time with MySQL are in the excellent online manual:

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-types.html

It does not accept an ISO formatted date string. However, it would appear trivial to replace the "T" with a space and to remove the trailing "Z".

Haven't used MySQL for years I'm afraid so I can't test it.

Hello Julian, thank you for your information. I was wondering, the node, I was using, is described as "Log MQTT to MySQL" and the Javascript in there does put out ISO-formatted timestamp. That is most probably the reason, why the search "Error: ER_TRUNCATED_WRONG_VALUE is all over the place, many of them using the exact same node as I did. So, I finally fixed it with just letting the database add the timestamp, that´s it.

Have a great day, regards, Jürgen

For one and a half day, the solution found is not satisfying, but, as I have mentioned, I found a workaround elsewhere. Have a nice day, regards, Jürgen

I see from a quick search that friend of the forum @hardillb answered a Stack Overflow question some time back and part of his suggestions were using a different MQTT broker that supports a more flexible approach to storage such as HiveMQ.

With that, it might be possible to get the MQ service to do the heavy lifting if all you need is for MQTT messages to be replicated to a database.

Either way, yes, it is nearly always better to let the database engine do as much of the work as possible rather than trying to pass in information. Also worth looking at prepared statements if you are needing to pass lots of data into a db quickly. They mean that the db engine has less work to do and so your writes are faster.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.