The "Multi-dimensional Single Cycle Mode" mentioned in the previous chapter mentioned the need to use the technology of splicing strings in the database, but when the splicing strings are too long and complex, it will be very complicated to disassemble the strings.
This is summarized in the background of Oracle, and other databases can be obtained in the same way.
CREATE OR REPLACE FUNCTION indchar (str1 varchar2, str2 varchar2,indexs number) RETURN varchar2 AS varlen1 number;-- the total length of the string varlen2 number;-- the length of the string used to store the string i number;-- used to record the position of each string in the string n number;--a string used to record several strings in the string m number;--Used to record the position of the nth string in the string used to truncate the string varstr1 varchar2(200);--temporary access needs to intercept a single string in the string varstr2 varchar2(200);--temporary access for a single string in the string varstr3 varchar2(200);-- used to store the return string BEGIN varlen1 := length(str1); varlen2 := length(str2); i := 1; n := 0; m := 0; --The loop is used to calculate how many strings are used for truncation
while i<= varlen1 loop varstr1 := substr(str1,i,1); varstr2 := substr(str2,1,1); if varstr1 = varstr1 then if substr(str1,i,varlen2) = str2 then n:= n+1; end if; end if; i :=i+1; end loop; ------ Determine the index position if indexs <=0 then return ''; end if; -- Determine the number of intercepted strings if n=0 then return str1; end if; -- Determine the index position if indexs = 1 then i:=1; n:=0; while i<= varlen1 loop varstr1 := substr(str1,i,1); varstr2 := substr(str2,1,1); varstr3 := concat(varstr3,substr(str1,i,1)); if varstr1 = varstr2 then if substr(str1,i,varlen2) = str2 then n:= n+1; return substr(varstr3,1,m-2); end if; end if; i :=i+1; end loop; end if; if indexs <= n+1 then ------- i:=0; n:=0; while i<= varlen1 loop varstr1 := substr(str1,i,1); varstr2 := substr(str2,1,1); --varstr3 := concat(varstr3,substr(str1,i,1)); if varstr1 = varstr2 then if substr(str1,i,varlen2) = str2 then n:= n+1; m:= i+1; --return substr(varstr3,1,m-2); end if; end if; if n+1 = indexs then varstr3 := concat(varstr3,substr(str1,i,1)); end if; if n+1 > indexs then return substr(varstr3,length(str2)+1,length(varstr3)-length(str2)); end if; i :=i+1; end loop; ------- end if;
----Get the last string after splitting return substr(varstr3,length(str2)+1,length(varstr3)-length(str2)+1); END;
-- Call function parameter description: --num_char(str1 varchar2,str2 varchar2,indexs number) --str1 original string --str2 split string --index string number index
The following gets the last string in the split string
--Query statement: select num_char('ws++rq++sl++dl','++',4) from dual --Return result: dl
The original text is in the programming forum: http://blog.bccn.net/mooncharmzx/65076
********Reprinted or quoted must indicate the source************