select regexp_substr('a,b,,cd,e,,efg,', '[^,]+', 1, 1) from dual
这样分隔出来是
a
b
cd
e
efg
如何使用双逗号分隔使得分隔出来的效果为?
a,b
cd,e
efg
https://wk.baidu.com/view/da413938660e52ea551810a6f524ccbff121ca79
这种你这个函数解决不了,
要写plsql代码
/*创建视图,方便查询 */
create or replace view example_test as select 'a,b,,cd,e,,efg,' example from dual;
-- 下面是函数过程
declare
i number;
j number;
k number;
type all_positiion is table of number index by binary_integer; -- 定义数组存储
position all_positiion ; -- 存储 ',,'出现的位置,由于',,'占2位,后面使用的时候相对+2或-2
find_str varchar2(100);
begin
-- 存储 双',,'出现总次数
select regexp_count(t.example,',,') into i from example_test t;
-- 这个循环是写入双',,' 出现的位置
for j in 1..i loop
select instrb(t.example,',,',1,j) into position(j) from example_test t;
end loop;
-- 这个根据双',,' 出现的位置, 截断字符串
for k in 1..position.count+1 loop
-- 如果k=1,即双',,' 出现的位置是第1次, 或者 k=position.first, 因为 position.first也是1
if k=position.first
then
select substr(t.example,1,position(k)-1 ) into find_str from example_test t;
dbms_output.put_line(find_str);
-- 如果出现在中间部分
elsif k>position.first and k<=position.last
then
select substr(t.example,position(k-1)+2,position(k)-position(k-1)-2 ) into find_str from example_test t;
dbms_output.put_line(find_str);
-- 排队上面2种情况,即出最后一次出现的位置[不一定是字符串尾部]
else
select substr(t.example,position(k-1)+2) into find_str from example_test t;
dbms_output.put_line(find_str);
end if;
end loop;
end;
输出结果
a,b
cd,e
efg,