[MDEV-21861] INSERT INTO ... SELECT ERROR Created: 2020-03-03  Updated: 2020-03-03  Resolved: 2020-03-03

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Insert
Affects Version/s: 10.2
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Bernd Buffen Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Environment:

Testing on some MariaDB Version and also MySQL


Issue Links:
Duplicate
duplicates MDEV-17800 STR_TO_DATE errors when used during a... Closed

 Description   

If i have a SELECT query that returns 1 from 2 ROWS everything is OK. But when i add it afer a INSERT it reports a ERROR on the second ROW

Sample CODE

CREATE TABLE `table1` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`dt_int` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `table1` (`id`, `dt_int`)
VALUES
(1, 20200202),
(2, 20202020);

CREATE TABLE `table2` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`dt` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;

– OK it returns only 1 correct ROW
SELECT DATE(str_to_date( dt_int , '%Y%m%d'))
FROM table1
WHERE date(str_to_date( dt_int, '%Y%m%d')) is not null;

– reports ERROR – Incorrect datetime value: '20202020' for function str_to_date
INSERT INTO table2(dt)
SELECT DATE(str_to_date( dt_int , '%Y%m%d'))
FROM table1
WHERE date(str_to_date( dt_int, '%Y%m%d')) is not null
;

How can i fix this in the query



 Comments   
Comment by Alice Sherepa [ 2020-03-03 ]

SELECT produces a warning, while INSERT returns an error because of strict sql_mode (https://mariadb.com/kb/en/sql-mode/#strict-mode)

MariaDB [test]> SELECT DATE(str_to_date(dt_int,'%Y%m%d')) FROM table1 WHERE date(str_to_date(dt_int,'%Y%m%d')) is not null;
+------------------------------------+
| DATE(str_to_date(dt_int,'%Y%m%d')) |
+------------------------------------+
| 2020-02-02                         |
+------------------------------------+
1 row in set, 1 warning (0.001 sec)
 
Warning (Code 1411): Incorrect datetime value: '20202020' for function str_to_date

Generated at Thu Feb 08 09:10:22 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.