首页 新闻 会员 周边

MYSQl自定义函数

0
悬赏园豆:30 [已解决问题] 解决于 2017-12-22 14:58


DELIMITER //


CREATE FUNCTION StrToTime(dateformat varchar(50),timeslot varchar(50),mark int)

Returns varchar(50)

Begin
declare year1 varchar(50);
declare month1 varchar(50);
declare day1 varchar(50);
declare hour1 varchar(50);
declare minminute varchar(50);
declare minute1 varchar(50);
declare str varchar(50);
declare returnstr varchar(50);
declare lennum int;

select lennum=len(dateformat)-len(replace(dateformat,'-',''));
set year1 =left(dateformat,charindex('-',dateformat)-1);
set str = stuff(dateformat,1,charindex('-',dateformat),'');
set returnstr='';

if ennum =3 and timeslot ='0' then
set month1= left(str,charindex('-',str)-1);
set str = stuff(str,1,charindex('-',str),'');
set day1 = left(str,charindex('-',str)-1);
set hour1 = stuff(str,1,charindex('-',str),'');
if mark =0 then
set returnstr = year1 + '-' + month1 + '-' + day1 + ' ' + hour1 + ':00:00';
else

set returnstr = year1 + '-' + month1 + '-' + day1 + ' ' + hour1 + ':59:59';
end if;
end if;
/********************** --年-月-日-时(有分钟) **********************/

if lennum =3 and timeslot !='0' then
set month1 = left(str,charindex('-',str)-1);
set str = stuff(str,1,charindex('-',str),'');
set day1 = left(str,charindex('-',str)-1);
set hour1 = stuff(str,1,charindex('-',str),'');
/** set minminute =convert(varchar,convert(int,left(timeslot,instr('-',timeslot)-1)));****/
set minminute =convert(convert(left(timeslot,instr('-',timeslot)-1),SIGNED),char);

if minminute='0' then
set minminute ='00';
else
set minute1 = convert(reverse(left(reverse(timeslot),charindex('-',reverse(timeslot))-1))-1,char);
if mark =0 then
set returnstr = year1 + '-' + month1 + '-' + day1 + ' ' + hour1 +':'+minminute+':00';

else
set returnstr = year1 + '-' + month1 + '-' + day1+ ' ' + hour1 +':'+minute1+ ':59';
end if;
end if;
/** 年-月-日 **/
if lennum =2 then
set month1 = left(str,charindex('-',str)-1);
set str = stuff(str,1,charindex('-',str),'');
set day1 = str;
if mark =0 then

set returnstr = year1 + '-' + month1 + '-' + day1 + ' ' + '00:00:00';
else

set returnstr = year1 + '-' + month1 + '-' + day1 + ' ' +'23:59:59';
end if;
end if;
/** 年-月**/
if lennum =1 then

set month1 = str;
if month1='12' then

set day1=31;

else

select day1= datediff(convert(year1+'-'+month1+'-'+'1',datetime),convert(year1+'-'+convert(convert(month1,signed)+1,char)+'-'+'1',datetime)) ;
end if;
if mark =0 then

set returnstr = dateformat + '-' + '1' + ' ' + '00:00:00';

else

set returnstr = dateformat + '-' + day1 + ' ' + '23:59:59';
end if;
end if;

Return returnstr;
End //
DELIMITER ;

 提示错误:谁帮忙看看  right syntax  to use  near '//' at line 90.

最好是  复制下来 执行下. 新人刚接触 mysql

漠明奇喵的主页 漠明奇喵 | 初学一级 | 园豆:46
提问于:2017-12-21 17:06
< >
分享
最佳答案
0

自己的 if语句 写错了. 晕,真难找~~ 眼花

漠明奇喵 | 初学一级 |园豆:46 | 2017-12-22 14:57

解决了就好

我只是慕名而来 | 园豆:303 (菜鸟二级) | 2017-12-25 16:35
其他回答(1)
0

错误提示你:在第90行使用的"//"是错误的,叫你正确使用

收获园豆:30
我只是慕名而来 | 园豆:303 (菜鸟二级) | 2017-12-21 19:27

大哥,我也懂提示。但是我看不懂 哪里写错了。

百度的案例 也是这么写的。  end 后面 加  前面定义的 符号. // .  

DELIMITER //   begin .....  end // DELIMITER ;  这种格式

支持(0) 反对(0) 漠明奇喵 | 园豆:46 (初学一级) | 2017-12-21 19:40

而且 我删了 //  也会提示我错误。只不过是 把//换成了 ''

right syntax  to use  near '' at line 90.

支持(0) 反对(0) 漠明奇喵 | 园豆:46 (初学一级) | 2017-12-21 19:43

@漠明奇喵: 我也找不到答案

支持(0) 反对(0) 我只是慕名而来 | 园豆:303 (菜鸟二级) | 2017-12-21 20:27

@我只是慕名而来: 嗯,那也感谢你抽时间看我的问题。

支持(0) 反对(0) 漠明奇喵 | 园豆:46 (初学一级) | 2017-12-21 20:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册