9

I was using int type for storing datetime type data. For the convenience of getting the data in specific range from MySQL, I tried changing it to TIMESTAMP/DATETIME but it is giving the error as in the attached image in both the cases. The format for datatype TIMESTAMP/DATETIME is YYYY-MM-DD HH:MM:SS which is 19 character long.

I could not get a proper tutorial/article to get knowledge about this error/issue.

Error Image

2
  • 2
    Don't get hang up on tutorials all the time. It makes much more sense to read up on stuff like this directly in the official documentation. dev.mysql.com/doc/refman/5.7/en/…
    – CBroe
    Jul 31, 2016 at 13:12
  • So the limitation 6 means that it is fractional value? I guess I will get the results if feed the database with YYYY-MM-DD HH:MM:SS. Thanks for pointing out this cruicial point regarding this datatype although I have gone through articles in dev.mysql.com. Jul 31, 2016 at 13:21

1 Answer 1

13

When defining a DATETIME or TIMESTAMP field, there's no need to specify the length.

This is what the error message refers to:

A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision

MySQL permits fractional seconds for TIME, DATETIME, and TIMESTAMP values, with up to microseconds (6 digits) precision. To define a column that includes a fractional seconds part, use the syntax type_name(fsp), where type_name is TIME, DATETIME, or TIMESTAMP, and fsp is the fractional seconds precision. For example:

CREATE TABLE t1 (t TIME(3), dt DATETIME(6));

Abstract from CREATE TABLE Syntax:

| TIME[(fsp)]
| TIMESTAMP[(fsp)]
| DATETIME[(fsp)]

Documentation:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.