create or replace function myquchong(oldStr varchar2) return varchar2 is
str varchar2(1000); --存放原始字符串
midstr varchar2(1000); --存放单个关键字,两个斜杠之间的
Result varchar2(1000); --存放结果字符串
startIndex number;
endIndex number;
i number;
begin
--如果传入的字符串为空,则直接返回空
if oldStr is null then
return ('');
end if;
--初始化参数
str := oldStr || ',';
startIndex := 0;
endIndex := 0;
i :=0;
loop
i :=i+1; --第一次执行时为1
startIndex := endIndex;
--获取斜杠第i次出现的位置
endIndex := instr(str, ',', 1, i);
if (endIndex = 0) then
exit;
end if;
--截取整个关键字,放在一个变量中
midstr :=trim(substr(str, startIndex + 1, endIndex - startIndex - 1));
--第一次赋值前,结果集为空。
if (Result is null) then
Result := midstr;
end if;
--第二次赋值前判断是否在结果集中已经存在,前后加斜杠确保是整个关键字匹配
if instr(',' || Result ||',',','|| midstr ||',')=0 then
Result :=Result || ',' || midstr;
end if;
end loop;
return(Result);
end myquchong;
str varchar2(1000); --存放原始字符串
midstr varchar2(1000); --存放单个关键字,两个斜杠之间的
Result varchar2(1000); --存放结果字符串
startIndex number;
endIndex number;
i number;
begin
--如果传入的字符串为空,则直接返回空
if oldStr is null then
return ('');
end if;
--初始化参数
str := oldStr || ',';
startIndex := 0;
endIndex := 0;
i :=0;
loop
i :=i+1; --第一次执行时为1
startIndex := endIndex;
--获取斜杠第i次出现的位置
endIndex := instr(str, ',', 1, i);
if (endIndex = 0) then
exit;
end if;
--截取整个关键字,放在一个变量中
midstr :=trim(substr(str, startIndex + 1, endIndex - startIndex - 1));
--第一次赋值前,结果集为空。
if (Result is null) then
Result := midstr;
end if;
--第二次赋值前判断是否在结果集中已经存在,前后加斜杠确保是整个关键字匹配
if instr(',' || Result ||',',','|| midstr ||',')=0 then
Result :=Result || ',' || midstr;
end if;
end loop;
return(Result);
end myquchong;
------------------------------------示例:
select myquchong('a,a,b,b,a,c') from dual;