# 1411 - Wrong date and time value for str_to_date function on INSERT INTO ... SELECT

These queries require converting a string to a date because the Timestamp is stored as a string and the registration application does not change.

I have a select query that works fine ->

(SELECT main.user_id, main.Timestamp FROM `user_table` main WHERE STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) < (SELECT MAX(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' )) FROM `user_table` sub WHERE sub.user_id = main.user_id )) 

This will select ALL, BUT the last user ID and timestamp added from my table.

However, when I try to insert this into another table ... something like this →

 INSERT INTO user_table_temp (`user_id`, `Timestamp`) (SELECT main.user_id, main.Timestamp FROM `user_table` main WHERE STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' ) < (SELECT MAX(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' )) FROM `user_table` sub WHERE sub.user_id = main.user_id )) 

I get the following error →

 #1411 - Incorrect datetime value: 'Mon Mar 14 16:10:10 CDT 2011' for function str_to_date 

The reason for this is that my timestamps are stored in two formats. One will be stored as "CST" during time saving, and the other will be stored as "CDT" during summer time. When an INSERT INTO SELECT hits the first line, which is the opposite of what I request, it will not work with the above message.

I also tried COALESCE them, which also works when running select ->

 INSERT INTO user_table_temp (`user_id `, `Timestamp`) (SELECT main.user_id , main.Timestamp FROM `user_table` main WHERE COALESCE(STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' )) < (SELECT MAX(COALESCE(STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CDT %Y' ), STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' ))) FROM `user_table` sub WHERE sub.user_id = main.user_id )) 

Why will this happen with INSERT, but SELECT will work?

+8
sql mysql datetime select insert
source share
1 answer

I think this does not work in INSERT because you are not using a supported format . Supported formats.,.

Like a string in the "YYYY-MM-DD HH: MM: SS" or "YY-MM-DD HH: MM: SS" format. The "relaxed" syntax is also allowed here: any punctuation character can be used as a separator between the dates or time of parts. For example, '2012-12-31 11:30:45', '2012 ^ 12 ^ 31 11 + 30 + 45', '2012/12/31 11 * 30 * 45' and '2012 @ 12 @ 31 11 ^ 30 ^ 45 'are equivalent.

Like a delimited string in "YYYYMMDDHHMMSS" or 'YYMMDDHHMMSS', provided that the string makes sense as a date. For example, "20070523091528" and "070523091528" are interpreted as' 2007-05-23 09:15:28 ', but' 071122129015 'is illegal (it has a meaningless minute part) and becomes' 0000-00-00 00:00:00 "

As a number in the format YYYYMMDDHHMMSS or YYMMDDHHMMSS, it is provided that the number makes sense as a date. For example, 19830905132800 and 830905132800 are interpreted as "1983-09-05 13:28:00".

0
source share

All Articles