MySQL datetime data saving error for 15+ years data - mysql

In MySQL 8.0.28 we have a table column named expiry_date as datetime filed.
When we try to insert data greater than 15 years, it fires
ERROR 1292: 1292: Incorrect datetime value: '2038-01-20 03:36:33' for column 'expiry_date' at row 1
For 2038-01-19 03:36:33 date, its inserting to DB.
Table ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
We can not change data-type as this table is in production system.
What is causing this issue?
Following threads were checked, but not addressing our issue :
incorrect-datetime-value-while-saving-data-to-mysql
incorrect-datetime-value-database-error-number-1292
error-in-mysql-when-saving-data

Related

MySQL 5.7.26 weird issue... cannot set datetime to: 2020-03-21 00:00:00

I cannot insert this date in my table: 2020-03-21!
I mean I can set 20 and 22 just fine, but when I set 21 it fails with Error:
Incorrect datetime value: '2020-03-21 00:00:00'
Using PDO I get:
PDOException::("SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2020-03-21 00:00:00' for column 'when' at row 1")
I'm really confused, so any help is appreciated!
Server version: 5.7.26 - MySQL Community Server (GPL)
Protocol version: 10
Table:
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`when` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
Queries:
This works just fine:
INSERT INTO `test` (`when`) VALUES ('2020-03-20 00:00:00');
This returns ERROR:
INSERT INTO `test` (`when`) VALUES ('2020-03-21 00:00:00');
Incorrect datetime value: '2020-03-21 00:00:00' for column 'when' at row 1
Same Error using phpMyAdmin
#MadhurBhaiya answered this as a comment,
The issue was "Daylight Saving", basically that time didn't exist for my server timezone.
Fixed it by changing timezone to UTC (which doesn't have Daylight Saving)
Also, please note that this issue only happens for TIMESTAMP field (doesn't happen for DATETIME)

What is the exact maximum value for TIMESTAMP in MySQL?

The documentation states that -
the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'
But when I get this when I try to enter the maximum value:
mysql> insert into integration_table (`TIMESTAMP`) VALUES ('2038-01-19 03:14:07.999999');
ERROR 1292 (22007): Incorrect datetime value: '2038-01-19 03:14:07.999999' for column 'TIMESTAMP' at row 1
If I incrementally decrease the value, the maximum value that will work is '2038-01-19 03:14:07.499999'
MySQL version is mysql Ver 8.0.11 for Linux on x86_64 (MySQL Community Server - GPL) (running in Docker)
Column definition is `TIMESTAMP` timestamp NULL DEFAULT NULL,
Table definition is ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I know that MySQL supports microseconds since version 5.6.4, so that's not the issue.
Am I misunderstanding something or is this some configuration issue?
Thanks
Defining the column as TIMESTAMP means it does not support microseconds, so .5 and above will be rounded up, which makes this value out of range.
The column needs to be defined as TIMESTAMP(6) to support 6 digit microseconds.

Invalid default value for 'createdAt' DATETIME DEFAULT NOW()

I am running the following query in mariadb/ mysql engine.
CREATE TABLE IF NOT EXISTSQuotes(idINTEGER NOT NULL auto_increment ,quoteTEXT,authorVARCHAR(255),genreVARCHAR(255),tagTEXT,createdAtDATETIME DEFAULT NOW(),updatedAtDATETIME DEFAULT NOW(), PRIMARY KEY (id)) ENGINE=InnoDB;
However, I am getting the following error:
An error occurred while creating the table:
{Invalid default value for 'createdAt'}
Here is the result of sql mode:
show variables like 'sql_mode' ;
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Any help is appreciated.
From https://mariadb.com/kb/en/library/datetime/ :
MariaDB starting with 10.0.1 Since MariaDB 10.0.1, DATETIME columns
also accept CURRENT_TIMESTAMP as the default value.
Note that NOW() is not mentioned, but the effect is the same.

How to update date field automatically from a timestamp field in Mysql 5.5?

I have an InnoDB table with a timestamp in it, and I wish to have another field which carries only the date part of the timestamp, so that I can create an index on it. (My temporal queries will always be bound by date, so having an index with high cardinality on the timestamp is not really needed.)
Is it possible to have the date field update automatically ON UPDATE from the timestamp field (similar to how CURRENT_TIMESTAMP works)?
I tried the following but it MySQL says I have an error in my SQL syntax.
CREATE TABLE test_table (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`full_ts` timestamp NULL DEFAULT NULL COMMENT 'The full timestamp',
`only_date` date NULL DEFAULT NULL ON UPDATE date(full_ts) COMMENT 'This field carries only the date part of the full timestamp for better indexing.',
PRIMARY KEY (`id`),
KEY `ONLY_DATE_IDX` (`only_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I could of course update both fields everywhere in the code, but it would be cleaner if the only_date field was a slave of the full_ts field, updated and kept consistent by the database.
I know that in MySQL 5.7.5 there was a new feature added for stored generated columns, which seem to do exactly this. Unfortunately it is not possible to upgrade the database version at the moment.
Is there a way to achieve this in MySQL 5.5?
This will update the "only_date" column when you update the "full_ts" column
CREATE TRIGGER `autoDate` BEFORE UPDATE ON `test_table` FOR EACH ROW BEGIN
SET NEW.only_date=DATE(NEW.full_ts);
END
EDIT:
For further reading on triggers, please refer to https://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
Also worth reading about triggers https://dev.mysql.com/doc/refman/5.5/en/faqs-triggers.html

Error in importing CSV data into MySQL database using PHPMyAdmin

I'm importing CSV file into database using PHPMyAdmin. I have kept null for the columns that I don't want to import values right now. When I import CSV it is giving me an error: #1292 - Incorrect datetime value: '' for column 'updated_at' at row 1.
Is there a way to want to get rid of this error without importing values into 'updated_at' column.
Note: 'updated_at' is a date time column. It is created using following query.
ALTER TABLE labdata1 ADD updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;