Mysql incorrect datetime value iso 8601

Error log:

{ [Error: Incorrect datetime value: '2012-08-24T17:29:11.683Z' for column 'robot _refreshed_at' at row 1] number: 1292, sqlStateMarker: '#', sqlState: '22007', message: 'Incorrect datetime value: \'2012-08-24T17:29:11.683Z\' for column \' robot_refreshed_at\' at row 1', sql: 'INSERT INTO users (id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers) VALUES (\'1834084\',\'NNNyingzi\',\'5\',\ '0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\')', setMaxListeners: [Function], emit: [Function], addListener: [Function], on: [Function], once: [Function], removeListener: [Function], removeAllListeners: [Function], listeners: [Function] }

I use this piece of code in my Node.js

  if s instanceof Date
         return s.toISOString()

and updated them in database.

The SQL insert expression follows:

     INSERT INTO users (id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers) VALUES (\'1834084\',\'NNNyingzi\',\'5\',\ '0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\')

Am I doing anything wrong? I just copied a table using PHPMyAdmin from a table in server.

Thanks a lot.

The MySQL Incorrect datetime value error (which is also known as ERROR 1292) is triggered when you perform an INSERT statement that contains one or more DATETIME values with the wrong format.

MySQL only accepts DATETIME values in the format of YYYY-MM-DD hh:mm:ss for string type or YYYYMMDDhhmmss for integer type.

To show you an example, suppose you have a table with DATE and DATETIME columns as shown below:

+-------------+--------------+------+
| Field       | Type         | Null |
+-------------+--------------+------+
| id          | int unsigned | NO   |
| join_date   | date         | YES  |
| last_update | datetime     | YES  |
+-------------+--------------+------+

Now suppose you want to INSERT a new value to the last_update column with the following statement:

INSERT INTO example (last_update) values("10-17-2021 15:40:10");

MySQL will throw an error as shown below:

ERROR 1292 (22007): Incorrect datetime value: '10-17-2021 15:40:10' 
for column 'last_update' at row 1

This is because the DATETIME value in the statement above uses the DD-MM-YYYY HH:MM:SS format, which is unacceptable by MySQL.

The obvious way to fix the error is to change the formatting of your value into the format that MySQL can accept.

But rather than editing the value manually, you can use the STR_TO_DATE() function to help you convert the string value into date value.

Here’s an example of STR_TO_DATE() function in action:

SELECT STR_TO_DATE("10-17-2021 15:40:10", "%m-%d-%Y %H:%i:%s");

-- 2021-10-17 15:40:10  

The STR_TO_DATE() function requires two arguments to run:

  • The datetime string that you want to convert
  • The format of the datetime string you pass to the function

Because there are many valid datetime formats in the world, it’s impossible for MySQL to guess what format the string value you passed to the function.

The STR_TO_DATE() function uses the same specifiers as the DATE_FORMAT() function that you can see here.

With the STR_TO_DATE() function, the previous INSERT statement won’t cause an error:

INSERT INTO example (last_update) 
  values(STR_TO_DATE("10-17-2021 15:40:10", "%m-%d-%Y %H:%i:%s"));

And that’s how you can fix the error Incorrect datetime value in MySQL.

Keep in mind that the error can also be triggered when you try to insert a DATE value to a DATE type column as follows:

mysql> INSERT INTO example (join_date) values("10-17-2021");

ERROR 1292 (22007): Incorrect date value: '10-17-2021' 
for column 'join_date' at row 1

The error will say Incorrect date value instead of Incorrect datetime value, but they are both the same error.

You can use STR_TO_DATE() to format your date value as shown below:

INSERT INTO example (join_date) 
  values(STR_TO_DATE("10-17-2021", "%m-%d-%Y"));

Just remember that you need to pass the right format to the STR_TO_DATE() function, or MySQL won’t be able to process your value.

In another example, if you’re using the forward slash / as the separator for your date value, then you need to use the same separator for the format parameter:

INSERT INTO example (join_date) 
  values(STR_TO_DATE("10/17/2021", "%m/%d/%Y"));

When you have many values that you want to insert to your table, using STR_TO_DATE function will save you from having to edit your values format manually.

But if you only want to insert a single value, then formatting your value manually might be faster.

Now you’ve learned how to fix the invalid datetime value error in MySQL database server. Great job! 👍

How do I fix incorrect datetime value in MySQL?

The obvious way to fix the error is to change the formatting of your value into the format that MySQL can accept. But rather than editing the value manually, you can use the STR_TO_DATE() function to help you convert the string value into date value.

How to fix error 1292 in MySQL?

How we fix MySQL Error 1292.
If a field type is a DATE, then we make sure that the date is entered in the format “yyyy-mm-dd”..
Error Code: 1292 – Incorrect date value. Many of our customers use MySQL 5.7. But in this version date value like 0000-00-00 00:00:00 is not allowed. ... .
# 1292 – Truncated incorrect DOUBLE value..

Can we change date format in MySQL?

MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.

How do you use timestamp?

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.