MySQL 插入日期时出错 - 日期值不正确:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11641096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:15:56  来源:igfitidea点击:

Error while inserting date - Incorrect date value:

mysqlsql

提问by Illep

I have a column called todayand the type is DATE.

我有一个名为列today和类型DATE

When I try to add the date in the format '07-25-2012'I get the following error:

当我尝试以格式添加日期时,'07-25-2012'出现以下错误:

Unable to run query:Incorrect date value: '07-25-2012' for column

无法运行查询:日期值不正确:列的“07-25-2012”

回答by Akash KC

As MySql accepts the date in y-m-d format in date type column, you need to STR_TO_DATEfunction to convert the date into yyyy-mm-dd format for insertion in following way:

由于MySql在日期类型列中接受ymd格式的日期,因此您需要通过STR_TO_DATE以下方式将日期转换为yyyy-mm-dd格式以进行插入:

INSERT INTO table_name(today) 
VALUES(STR_TO_DATE('07-25-2012','%m-%d-%y'));  

Similary, if you want to select the date in different format other than Mysql format, you should try DATE_FORMATfunction

类似地,如果你想选择 Mysql 格式以外的不同格式的日期,你应该尝试DATE_FORMAT函数

SELECT DATE_FORMAT(today, '%m-%d-%y') from table_name; 

回答by aserwin

Generally mysql uses this date format 'Y-m-d H:i:s'

通常 mysql 使用这种日期格式 'Ymd H:i:s'

回答by Ike Walker

You need to convert the date to YYYY-MM-DD in order to insert it as a MySQL date using the default configuration.

您需要将日期转换为 YYYY-MM-DD,以便使用默认配置将其作为 MySQL 日期插入。

One way to do that is STR_TO_DATE():

一种方法是STR_TO_DATE()

insert into your_table (...)
values (...,str_to_date('07-25-2012','%m-%d-%Y'),...);

回答by Robert Forderer

I had a different cause for this error. I tried to insert a date without using quotes and received a strange error telling me I had tried to insert a date from 2003.

我有这个错误的不同原因。我试图在不使用引号的情况下插入一个日期并收到一个奇怪的错误,告诉我我试图插入一个 2003 年的日期。

My error message:

我的错误信息:

Although I was already using the YYYY-MM-DD format, I forgot to add quotes around the date. Even though it is a date and not a string, quotes are still required.

虽然我已经在使用 YYYY-MM-DD 格式,但我忘记在日期周围添加引号。即使它是日期而不是字符串,仍然需要引号。

回答by Jacob

This is the date format:

这是日期格式

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD'format. The supported range is '1000-01-01' to '9999-12-31'.

DATE 类型用于具有日期部分但没有时间部分的值。MySQL 以'YYYY-MM-DD'格式检索和显示 DATE 值。支持的范围是“1000-01-01”到“9999-12-31”。

Why do you insert '07-25-2012' format when MySQL format is '2012-07-25'?. Actually you get this error if the sql_modeis traditional/strict modeelse it just enters 0000-00-00 and gives a warning: 1265 - Data truncated for column 'col1' at row 1.

当 MySQL 格式为“2012-07-25”时,为什么要插入“07-25-2012”格式?。实际上,如果sql_mode是,traditional/strict mode否则您会收到此错误,否则它只会输入 0000-00-00 并发出警告:1265 - Data truncated for column 'col1' at row 1

回答by Deepak Goel

you need to use YYYY-MM-DD format to insert date in mysql

您需要使用 YYYY-MM-DD 格式在 mysql 中插入日期

回答by dgsuraj

You can use "DATE" as a data type while you are creating the table. In this way, you can avoid the above error. Eg:

您可以在创建表时使用“DATE”作为数据类型。这样就可以避免上面的错误。例如:

CREATE TABLE Employee (birth_date DATE);
INSERT INTO Employee VALUES('1967-11-17');