错误代码:1411。日期时间值不正确:函数 str_to_date 的“0000-00-00”
Error Code: 1411. Incorrect datetime value: '0000-00-00' for function str_to_date
我想在插入之前更改 CSV 文件中日期的格式。我在插入之前创建了一个触发器,但它似乎不起作用。
CREATE TABLE items (
item_id int(11) NOT NULL AUTO_INCREMENT,
price_list_id int(11) NOT NULL,
sku varchar(20) NOT NULL,
description varchar(45) DEFAULT NULL,
cost float NOT NULL DEFAULT '0',
notes varchar(45) DEFAULT NULL,
discount_factor float DEFAULT '1',
start_date date DEFAULT NULL,
end_date date DEFAULT NULL,
PRIMARY KEY (item_id,sku),
KEY price_list_id_idx (price_list_id),
CONSTRAINT price_list_id FOREIGN KEY (price_list_id) REFERENCES price_lists (price_list_id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=105193 DEFAULT CHARSET=utf8;
这是我插入前的触发器
CREATE TRIGGER `pricelist`.`items_BEFORE_INSERT` BEFORE INSERT ON `items` FOR EACH ROW
BEGIN
SET NEW.start_date = str_to_date(NEW.start_date, '%c/%e/%Y');
SET NEW.end_date = str_to_date(NEW.end_date, '%c/%e/%Y');
END
假设查询是:INSERT IGNORE INTO pricelist.items (price_list_id, sku, description, cost, start_date, end_date, notes) VALUES ('15', '2494-22', 'M12 DRILL/IMPACT COMBO KIT', '129', '4/25/2016', '5/31/2016', 'CL6204');
我收到此错误:
`Error Code: 1411. Incorrect datetime value: '0000-00-00' for function str_to_date`
如果我这样做(不忽略):
INSERT INTO pricelist.items (price_list_id, sku, description, cost, start_date, end_date, notes) VALUES ('15', '2494-22', 'M12 DRILL/IMPACT COMBO KIT', '129', '4/25/2016', '5/31/2016', 'CL6204');
我明白了:
Error Code: 1292. Incorrect date value: '4/25/2016' for column 'start_date' at row 1
尽管这有效:
SELECT str_to_date('5/31/2016', '%c/%e/%Y');
输出为:
2016-05-31
如有任何帮助,我们将不胜感激!谢谢。
您收到此错误是因为您试图插入格式错误的 date
列。 MySQL documentation 列出可接受的格式:
MySQL recognizes DATE values in these formats:
As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31', '2012/12/31', '2012^12^31', and '2012@12@31' are equivalent.
As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date.
As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date.
但是您的日期格式为 MM/DD/YYYY
,例如4/25/2016
,所以这行不通。您似乎在尝试使用触发器在插入之前更正格式。但是,MySQL 在 触发之前 检查格式 。
如果您必须使用这种格式插入日期数据,那么您应该使用 VARCHAR
类型,然后调用 STR_TO_DATE
。或者,您应该清理日期格式以匹配其中一种可接受的格式。
我想在插入之前更改 CSV 文件中日期的格式。我在插入之前创建了一个触发器,但它似乎不起作用。
CREATE TABLE items (
item_id int(11) NOT NULL AUTO_INCREMENT,
price_list_id int(11) NOT NULL,
sku varchar(20) NOT NULL,
description varchar(45) DEFAULT NULL,
cost float NOT NULL DEFAULT '0',
notes varchar(45) DEFAULT NULL,
discount_factor float DEFAULT '1',
start_date date DEFAULT NULL,
end_date date DEFAULT NULL,
PRIMARY KEY (item_id,sku),
KEY price_list_id_idx (price_list_id),
CONSTRAINT price_list_id FOREIGN KEY (price_list_id) REFERENCES price_lists (price_list_id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=105193 DEFAULT CHARSET=utf8;
这是我插入前的触发器
CREATE TRIGGER `pricelist`.`items_BEFORE_INSERT` BEFORE INSERT ON `items` FOR EACH ROW
BEGIN
SET NEW.start_date = str_to_date(NEW.start_date, '%c/%e/%Y');
SET NEW.end_date = str_to_date(NEW.end_date, '%c/%e/%Y');
END
假设查询是:INSERT IGNORE INTO pricelist.items (price_list_id, sku, description, cost, start_date, end_date, notes) VALUES ('15', '2494-22', 'M12 DRILL/IMPACT COMBO KIT', '129', '4/25/2016', '5/31/2016', 'CL6204');
我收到此错误:
`Error Code: 1411. Incorrect datetime value: '0000-00-00' for function str_to_date`
如果我这样做(不忽略):
INSERT INTO pricelist.items (price_list_id, sku, description, cost, start_date, end_date, notes) VALUES ('15', '2494-22', 'M12 DRILL/IMPACT COMBO KIT', '129', '4/25/2016', '5/31/2016', 'CL6204');
我明白了:
Error Code: 1292. Incorrect date value: '4/25/2016' for column 'start_date' at row 1
尽管这有效:
SELECT str_to_date('5/31/2016', '%c/%e/%Y');
输出为:
2016-05-31
如有任何帮助,我们将不胜感激!谢谢。
您收到此错误是因为您试图插入格式错误的 date
列。 MySQL documentation 列出可接受的格式:
MySQL recognizes DATE values in these formats:
As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31', '2012/12/31', '2012^12^31', and '2012@12@31' are equivalent.
As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date.
As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date.
但是您的日期格式为 MM/DD/YYYY
,例如4/25/2016
,所以这行不通。您似乎在尝试使用触发器在插入之前更正格式。但是,MySQL 在 触发之前 检查格式 。
如果您必须使用这种格式插入日期数据,那么您应该使用 VARCHAR
类型,然后调用 STR_TO_DATE
。或者,您应该清理日期格式以匹配其中一种可接受的格式。