MySQL 1292 Incorrect datetime value - mysql

I am getting this error when I try to insert '2011/03/13 02:53:50.000000000' into a timestamp column. If I change the 13 to a 15, 14, 12 or 11 it works no problem. I've also tried changing the /'s to -'s and still no-go.
I've looked through some of the other threads related to this error but none seem to apply.
I'm running version 5.7.9.

It took me a while to figure this out...
The problem is that '2011-03-13 02:53:50' is illegal because of daylight saving time switch between 2 and 3 AM, so all time values between 2 and 3 am on any DST introduction day are invalid. Same for '2016-03-13 02:32:21', etc.
Change the system timezone to the one that does not use DST and you should be fine.

You need to try this:
STR_TO_DATE( '2011/03/13 02:53:50', '%Y/%m/%d %H:%i:%s')
or else you have to insert the dates using the dash seperator (-) like
'2011-03-13 02:53:50'
SQL FIDDLE DEMO

Still not sure what the issue is/was, maybe a combination of CentOS and MySQL versions. I changed the column to datatime(6) instead of timestamp(6) and I was able to import all my data successfully.

I think you need to use some str conversions in MySQL before inserting. Or to prepare the data in the proper format, before making the query to MySQL.
The microseconds format is also wrong. MySQL documentation clearly states this:
A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.
UPDATE: on my localhost I've got the same version of MySQL, and it works.
Tryed to execute conversion
select str_to_date("2011-03-13 02:53:50.000000", "%Y-%m-%d %H:%i:%s.%f") as `t`
and gotten:
+----------------------------+
| t |
+----------------------------+
| 2011-03-13 02:53:50.000000 |
+----------------------------+
1 row in set (0.00 sec)
Here's the SQLFiddle, that confirms the thing on other version of MySQL.
I run out of ideas, I think the issue is connected to the "local glitch" in Table structure or specific version of MySQL+OS.

1292 (22007): Incorrect datetime value: '2004-10-11 19:08:58.503079+05:30' for column grideye.alerts.timestamp at row 1
if you are getting the above error must try this
I written,
timestamp = parser.parse(data.get('timestamp'))
I tried this and this worked for me
timestamp = datetime.strptime(data.get('timestamp'), '%Y-%m-%dT%H:%M:%S.%f%z').strftime('%Y-%m-%d %H:%M:%S')

Related

MySQL time duration format, import from CSV

What's wrong?
A new column (in Excel) was made for calculating Duration
Using formula to subtract values from 2 cells
For example: 2021-01-01 07:30 - 2021-01-01 07:00; Duration = 00:30:00
In MySQL table, I set the data type for the column as TIME, but it would return with error when importing
Error message: 'Error Code: 1292. Incorrect datetime value: '00:13:00 ' for column 'ride_length'
What I have tried:
Changing data type to DATETIME, INT, TIME -> None worked
Changing the data format to HH:MM:SS, HHMMSS, 'YYYY-MM-dd HH:MM:SS' in CSV -> None worked
Question
What kind of data type I should set in MySQL?
If it was the CSV file's problem, what kind of 'data format' I need to set?
Thank you very much
Actually, it might be easier for you to just import the two datetime values and then use TIMSTAMPDIFF inside MySQL:
SELECT TIMESTAMPDIFF(MINUTE, '2021-01-01 07:00:00', '2021-01-01 07:30:00')
-- 30
Note that because the difference is actually a computed quantity, it generally makes more sense to compute it when you select. This way, should one of the two values be updated later, you don't have to worry about maintaining the difference column.

How to avoid error on MYSQL CONVERT() method, when passing a 0 second string and subtract from that value?

I am using MySQL 8 and have a problem with this type of query:
INSERT INTO review (name, create_date) VALUES('name', CONVERT(timestamp, DATETIME) - 1)
I have not had this error when using this expression in a where clause.
When the value for the timestamp is like '2020-12-16 06:15:01' it's working.
But with a value of 0 seconds (like: '2020-12-16 06:15:00') an error is dropped.
Incorrect datetime value: '20201216061499' for column 'create_date' at row 1
code: ER_TRUNCATED_WRONG_VALUE
errno: 1292
sqlState: 22007
I used this type of expression in my whole project. Is there a simple solution to this problem, without changing each expression?
Is that one a bug?
One solution to this problem is:
DATE_SUB(CONVERT(timestamp,DATETIME) INTERVAL 1 SECOND).
But as I already mention this requires changing each expression.
You do need to update each expression. When you subtract a number from your timestamp, it first converts your timestamp into a number (e.g. 20201216061500), then you are subtracting one and, because the column you are inserting is a datetime, it tries to interpret the resulting number as a date/time, failing when the subtraction produced 20201216061499. The correct way to subtract one second is to say - INTERVAL 1 SECOND or use DATE_SUB(..., INTERVAL 1 SECOND).

How to format string to time in mysql?

SELECT STR_TO_DATE('07:15:00','%H:%i:%s') as time
SELECT STR_TO_DATE('07:15:00','%T') as time
Both results in NULL. But why? I don't think the format is incorrect here, but why is my time nor parsed?
+------+
| time |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.01 sec)
Moreover, I only care for the hours and minutes. But this also fails:
SELECT STR_TO_DATE('07:15:00','%H:%i') as time
My final goal is to create a view, selecting strings from another table and converting them to TIME columns:
CREATE VIEW
myview AS SELECT
SELECT STR_TO_DATE('07:15:00','%H:%i') as time
FROM `othertable`
I suppose you're using version 5.7.
Look at SELECT ##GLOBAL.sql_mode if NO_ZERO_DATE value returns within the string. [ Btw, NO_ZERO_IN_DATE and NO_ZERO_DATE parameters are deprecated ]
If you're using Database at NO_ZERO_DATE, then SELECT STR_TO_DATE('07:15:00','%H:%i:%s') or SELECT STR_TO_DATE('07:15:00','%T') returns null.
Try to disable that mode by SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION'; if you have privilege, or you can edit mysql.cnf file and restart mysql.
Cast the time strings to time:
SELECT cast('07:15:00' as time) as time
If you cannot disable NO_ZERO_DATE, then here is a workaround you may use which still goes through STR_TO_DATE to generate time values. You may concatenate to the time a random date, and then call TIME() to extract only the time portion, e.g.
SELECT TIME(STR_TO_DATE(CONCAT('2019-01-01 ', '07:15:00'), '%Y-%m-%d %H:%i:%s'))
-- ^^ random date ^^ your time literal
SELECT DATE_FORMAT('2019-11-05','%Y%m%d') as time
try this function

mysql time_format function qustion

I insert and update mysql time_format function to express string value in time,
Why do some numbers get
"truncated incorrect time value"
errors?
For example, '55' is not a problem with query, but when '188' is entered, the above error message appears.
The type is VARCHAR (50).
my query :
INSERT INTO TABLE_HOME (DURATION)
VALUES (TIME_FORMAT (# {DURATION, jdbcType = VARCHAR}, '% H:% i:% s'))
UPDATE TABLE_HOME SET DURATION = TIME_FORMAT (# {DURATION, jdbcType = VARCHAR}, '% H:% i:% s')
Time_Format not all integers are valid..
188 is not a valid time anymore
Even where you placed it:
"%H= Hour => 188?
%i=Minute => 188?
%s"=Second => 188?
Do you think 188 is a valid time of an Hour, Minute, Second?
TIME_FORMAT() expects a time. If you feed it with anything else, you first get a cast. In this case:
mysql> SELECT CAST(55 AS TIME), CAST(188 AS TIME);
+------------------+-------------------+
| CAST(55 AS TIME) | CAST(188 AS TIME) |
+------------------+-------------------+
| 00:00:55 | NULL |
+------------------+-------------------+
1 row in set, 1 warning (0.00 sec)
The rules are:
MySQL recognizes TIME values in these formats:
As a string in 'D HH:MM:SS' format. You can also use one of the following “relaxed” syntaxes: 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH',
or 'SS'. Here D represents days and can have a value from 0 to 34.
As a string with no delimiters in 'HHMMSS' format, provided that it makes sense as a time. For example, '101112' is understood as
'10:11:12', but '109712' is illegal (it has a nonsensical minute part)
and becomes '00:00:00'.
As a number in HHMMSS format, provided that it makes sense as a time. For example, 101112 is understood as '10:11:12'. The following
alternative formats are also understood: SS, MMSS, or HHMMSS.
In this case, #3 applies:
55 is rendered as SS so it's valid.
188 is not a supported format so it produces NULL.
Date and time handling is already hard enough. I suggest to:
Be explicit to avoid ambiguity (something like 23:30:45 is crystal clear, 188 is open to interpretations).
Not use VARCHAR columns to store dates and times.

Remove milliseconds in a datetime field

I converted a database from MS SQL to MySql using workbench. There is a table that has a column called ActivityDate (datetime(6)) . For some reason, when that column got converted it has a dot in the date like (2013-05-03 11:20:20.420000) .
I want to remove the .420000 or whatever number is after the dot. I tried doing SUBSTRING_INDEX(ActivityDate,'.',1) but that didn't work, the last digits would just be .000000
I also tried UPDATEalerts.activitylogSETActivityDate= date_format(ActivityDate, '%Y-%m-%d %H:%i') WHEREactivitylog.ActivityLogID= 5;
And same issue... I get .000000 at the end
How can I do this?
Simply change the data type of the column to exclude the fractional part.
ALTER TABLE alerts.activitylog MODIFY ActivityDate DATETIME;
The type datetime(6) means 6 digits after the decimal point.
See the MySQL date and time fractional support documentation for details.
If you just want to select the datetime without the ms (like I was when I found this question) then the below CAST does the job:
SELECT CAST(created_at AS DATETIME) AS created_at_without_ms