创建函数中的语法错误

发布于 2024-09-15 08:21:52 字数 1000 浏览 2 评论 0原文

这是我尝试过的最令人困惑的功能之一。该逻辑在 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 function checkdate (
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-09-22 08:21:52

末尾有一个流氓逗号

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'
    ) /*<-- Comma Removed from Here*/
 ), 
    '%y%m%d');
return ret_val;
end$

A rogue comma right at the end

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'
    ) /*<-- Comma Removed from Here*/
 ), 
    '%y%m%d');
return ret_val;
end$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文