1.mysql函数: 判断一个字符串里面包含几个‘;’
drop function if exists func_containumsplit;
create function func_containumsplit(targetstr varchar(500))
returns INT
begin-- 函数头
DECLARE total INT DEFAULT 0;
DECLARE endnum INT DEFAULT 1;
SET targetstr=SUBSTR(targetstr FROM 2 FOR LENGTH(targetstr));
while endnum<>0 DO -- 循环开始
set endnum=(select POSITION(';' IN targetstr));
set targetstr=(select SUBSTR(targetstr FROM endnum+1));
set total=total+1;
end while;
return total-1;
end;
调用方法:
select func_containumsplit('65f37497-8edc-4e2f-91f6-47f8d8ac901d;19f69ea8-be70-47cf-a980-3e292577a288;19f69ea8-be70-47cf-a980-3e292577a288')
判断字符串中包含几个‘;’ 返回结果 2
2.mysql函数判断一个字符串里面是否包含‘;’
drop function if exists func_issplit;
create function func_issplit(targetstr varchar(500))
returns varchar (255)
begin-- 函数头
DECLARE total INT DEFAULT 0;
SET targetstr=SUBSTR(targetstr FROM 2 FOR LENGTH(targetstr));
set @num=(select POSITION(';' IN targetstr));
if(@num>10) then return 1;
ELSE return 0;
end if;
end;
调用方法:
select func_issplit('65f37497-8edc-4e2f-91f6-47f8d8ac901d;19f69ea8-be70-47cf-a980-3e292577a288;19f69ea8-be70-47cf-a980-3e292577a288')
返回结果 1,如果不包含;返回结果 0
3.mysql 根据索引对字符串进行分割
drop function if exists func_split;
create function func_split(targetstr varchar(500),indexnum INT)
returns varchar (255)
begin-- 函数头
declare i int default 0;
declare beginnum int DEFAULT 0;
while i<=indexnum DO -- 循环开始
set @endnum=(select POSITION(';' IN targetstr));
if(i+1<=indexnum) then
set targetstr=(select SUBSTR(targetstr FROM @endnum+1));
ELSEIF(@endnum<>0) THEN
set targetstr=(select left(targetstr,@endnum-1));
ELSE
set targetstr=targetstr;
end if;
set i=i+1;
end while;
return targetstr;
end;
调用方法:(索引从0开始)
select func_split('65f37497-8edc-4e2f-91f6-47f8d8ac901d;19f69ea8-be70-47cf-a980-3e292577a288;19f69ea8-be70-47cf-a980-3e292577a283',1);
返回结果:
19f69ea8-be70-47cf-a980-3e292577a288