CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN str_split
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;

创建完毕后,我们来测试一下,例如执行如下SQL:

select * from table(splitstr('Hello,Cnblogs!',','));

其输出结果为一个两行的表,如下图:

oracel 拆分字符串-LMLPHP

将行转为列显示:

select a.column_value v1,b.column_value v2 from
(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,
(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) b
where a.rn=1 and b.rn=2

如图:

oracel 拆分字符串-LMLPHP

作者:李敬然(Gnie) 出处:{GnieTech} (http://www.cnblogs.com/gnielee/)
 
 
方法二、
select regexp_substr('Hello,Cnblogs!','[^,]+',1,level) from dual connect by level <= length('Hello,Cnblogs!')-length(REPLACE('Hello,Cnblogs!',',',''))+1;
05-11 13:52