一、拆分多个字符串:
1.创建一个object类型,这个类型有几列,定义如下:
create or replace type mytypeobject as object
(
p_string_1 varchar(1000),
p_string_2 varchar(1000)
)
;
2.创建一个表类型,表的列是有上面的object类型
create or replace type str_splittableTwo as table of mytypeobject;
3.创建函数
CREATE OR REPLACE FUNCTION splitTwostr(p_string1 IN VARCHAR2,
p_string2 IN VARCHAR2,
p_delimiter IN VARCHAR2)
RETURN str_splittableTwo
PIPELINED AS
v_mytype mytypeobjectTwo;
v_length1 NUMBER := LENGTH(p_string1);
v_start1 NUMBER := 1;
v_index1 NUMBER;
v_length2 NUMBER := LENGTH(p_string2);
v_start2 NUMBER := 1;
v_index2 NUMBER;
BEGIN
WHILE (v_start1 <= v_length1) LOOP
v_index1 := INSTR(p_string1, p_delimiter, v_start1);
v_index2 := INSTR(p_string2, p_delimiter, v_start2);
IF v_index1 = 0 THEN
v_mytype := mytypeobjectTwo(SUBSTR(p_string1, v_start1),
SUBSTR(p_string2, v_start2));
PIPE ROW(v_mytype);
v_start1 := v_length1 + 1;
v_start2 := v_length2 + 1;
ELSE
v_mytype := mytypeobjectTwo(SUBSTR(p_string1,
v_start1,
v_index1 - v_start1),
SUBSTR(p_string2,
v_start2,
v_index2 - v_start2));
PIPE ROW(v_mytype);
v_start1 := v_index1 + 1;
v_start2 := v_index2 + 1;
END IF;
END LOOP;
RETURN;
END splitTwostr;
二、拆分一个字符串
1.只需要创建一个表类型,每行类型按需求自定义
CREATE OR REPLACE TYPE mytypelist IS TABLE OF VARCHAR2 (4000);
2.创建函数
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN
mytypelist
PIPELINED
AS
v_length NUMBER := LENGTH(p_string);
v_start NUMBER := 1;
v_index NUMBER;
BEGIN
WHILE(v_start <= v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);
IF v_index = 0
THEN
PIPE ROW(SUBSTR(p_string, v_start));
v_start := v_length + 1;
ELSE
PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
v_start := v_index + 1;
END IF;
END LOOP;
RETURN;
END splitstr;
调用:
select * from table(splitstr('Hello,Cnblogs!',','));