Insert into table using select based on date does not work
Hello,
when testing a select query like : select SYSD FROM Table fwt WHERE fwt.ACTIVITY = 'Close' AND STR_TO_DATE(fwt.TIMESTAMP,'%Y%m%d %H%i%s') >= now();
it returns line correctly, while trying the same select to insert data like: Insert into tablebis select SYSD FROM Table fwt WHERE fwt.ACTIVITY = 'Close' AND STR_TO_DATE(fwt.TIMESTAMP,'%Y%m%d %H%i%s') >= now();
I get the following error: SQL Error [1292] [22007]: (conn=6528) Truncated incorrect datetime value: '20200809 020917076'
Is it a know behavior? anyway to bypass that?
Thank you, Regards, Roland Roche
Answer Answered by Ian Gilfillan in this comment.
Your example seems to contain microseconds, but you do not cater for this in your formatting. Even the original query gives a warning in this case:
select STR_TO_DATE('20200809 020917076','%Y%m%d %H%i%s'); +---------------------------------------------------+ | STR_TO_DATE('20200809 020917076','%Y%m%d %H%i%s') | +---------------------------------------------------+ | 2020-08-09 02:09:17 | +---------------------------------------------------+ 1 row in set, 1 warning (0.000 sec) Warning (Code 1292): Truncated incorrect datetime value: '20200809 020917076'
You can use %f
to format for microseconds.
select STR_TO_DATE('20200809 020917076','%Y%m%d %H%i%s%f'); +-----------------------------------------------------+ | STR_TO_DATE('20200809 020917076','%Y%m%d %H%i%s%f') | +-----------------------------------------------------+ | 2020-08-09 02:09:17.076000 | +-----------------------------------------------------+