插入日期时出错-日期值不正确: [英] Error while inserting date - Incorrect date value:

查看:71
本文介绍了插入日期时出错-日期值不正确:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列称为today的列,类型为DATE.

I have a column called today and the type is DATE.

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

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

推荐答案

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

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

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

Similary,如果要选择Mysql格式以外的其他格式的日期,则应尝试使用DATE_FORMAT函数

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

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

这篇关于插入日期时出错-日期值不正确:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆