mysql 函式 判斷一個字串裡面包含幾個其他的字元
阿新 • • 發佈:2018-11-14
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