创建函数中的语法错误
这是我尝试过的最令人困惑的功能之一。该逻辑在 select 语句中工作,但我似乎无法创建用户定义的函数。
这是错误:
“您的 SQL 语法有错误; 检查对应的手册 您的 MySQL 服务器版本 在“分隔符$$”附近使用正确的语法 创建函数
checkdate
( p_datetimestamp varchar (6) ) 返回' 在第 1 行”
这是代码: (将文本字符串“yymmdd”更改为日期格式。根据逻辑,“100810”变为 2010-09-01。)
delimiter$$
create function `checkdate` (
p_datetimestamp varchar (6)
)
returns date
begin
declare ret_val date;
set ret_val =
str_to_date(
concat(
/*Assign Year -> %y*/
if(
mid(p_datetimestamp,3,2) = '12',
(left(p_datetimestamp,2) + 1),
left(p_datetimestamp,2)
),
/*Assign Month -> %m*/
if(
(mid(p_datetimestamp,3,2) >= '01' and mid(p_datetimestamp,3,2) <= '11'),
concat('0', (mid(p_datetimestamp,3,2) + 1)), '01'
),
/*Assign Date -> %d*/
if(
(mid(p_datetimestamp,5,2) >= '01' and mid(p_datetimestamp,5,2) <= '16'), '01', '17'
),
),
'%y%m%d');
return ret_val;
end$$
This is one of the more confusing functions I've attempted. The logic works within a select statement, but I can't seem to create a user-defined function.
Here's the error:
"You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'delimiter$$
create functioncheckdate
(
p_datetimestamp varchar (6) ) retur'
at line 1"
Here's the code:
(Changing a text string 'yymmdd' into date format. '100810' becomes 2010-09-01 per the logic.)
delimiter$
create function `checkdate` (
p_datetimestamp varchar (6)
)
returns date
begin
declare ret_val date;
set ret_val =
str_to_date(
concat(
/*Assign Year -> %y*/
if(
mid(p_datetimestamp,3,2) = '12',
(left(p_datetimestamp,2) + 1),
left(p_datetimestamp,2)
),
/*Assign Month -> %m*/
if(
(mid(p_datetimestamp,3,2) >= '01' and mid(p_datetimestamp,3,2) <= '11'),
concat('0', (mid(p_datetimestamp,3,2) + 1)), '01'
),
/*Assign Date -> %d*/
if(
(mid(p_datetimestamp,5,2) >= '01' and mid(p_datetimestamp,5,2) <= '16'), '01', '17'
),
),
'%y%m%d');
return ret_val;
end$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
末尾有一个流氓逗号
A rogue comma right at the end