【发布时间】:2014-11-26 01:08:17
【问题描述】:
我创建了一个函数,它以与原始格式不同的格式返回日期。
基本上,我正在使用这个Select MonthSub('2014-04-10',2)# 语句进行测试,它应该返回
2014-02,而不是2014-02-10。
有人可以检查我的代码,看看我做错了什么吗?
如果我使用date_format(new_in_date, '%y-%m') 对格式进行任何操作,则会返回此错误:
ERROR 1292 (22007): Incorrect date value: '2014-02' for column 'new_in_date' at row 1
我写的函数:
Create function MonthSub (in_date date, in_mn_adjust int)
returns date
Begin
declare new_in_date date default in_date;
set new_in_date := date_sub(new_in_date, interval in_mn_adjust month);
return new_in_date;
end;
【问题讨论】:
-
您的意思是标题中的 yyyy-mm-dd?
-
你想要的是,给定一个日期,减去月数,只返回年月。我理解正确吗? (例如:
MonthSub('2014-04-10', 2)表示“减去两个月并仅返回yyyy-mm;在本例中为2014-02) -
如果要在函数中使用
date_format,需要returns varchar