CREATE OR REPLACE TYPE "TY_STR_SPLIT" IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION fn_split(p_str IN VARCHAR2,
p_delimiter IN VARCHAR2)
RETURN ty_str_split
--功能:把字符串按照特定的分隔符转换为数组
/*调用例子
DECLARE
V_ARRY_RESULT TY_STR_SPLIT;--定义数组
BEGIN
V_ARRY_RESULT := FN_SPLIT(P_STR => '123|456|789', P_DELIMITER => '|');--调用把结果附给数组
END;*/
IS
j INT := 0;
i INT := 1;
len INT := 0;
len1 INT := 0;
str VARCHAR2(4000);
str_split ty_str_split := ty_str_split();
BEGIN
len := length(p_str);
len1 := length(p_delimiter);
WHILE j < len LOOP
j := instr(p_str, p_delimiter, i);
IF j = 0 THEN
j := len;
str := substr(p_str, i);
str_split.extend;
str_split(str_split.count) := str;
IF i >= len THEN
EXIT;
END IF;
ELSE
str := substr(p_str, i, j - i);
i := j + len1;
str_split.extend;
str_split(str_split.count) := str;
END IF;
END LOOP;
RETURN str_split;
END fn_split;