隐藏

Oracle函数,按分隔符截取字符串

发布:2015/11/11 1:57:10作者:管理员 来源:本站 浏览次数:1585

今天因工作需要,写了一个小函数,按分隔符截取字符串的,分享出来,希望有需要的朋友能用到。

功能实例:  substrbysep('aaa,bb,ccccc,ddd,vvv',',',3)返回结果 'ccccc'.

函数:

create or replace function substrbysep(sourceString    varchar2,
                                       destString      varchar2,
                                       appearPosition  number)
  return varchar2 is
  substring varchar2(255);
  
  --作者:Alex Liu
  --功能:按分隔符截取字符串
  --参数:1、原始字符串;2、分隔符;3、截取第几段
  
begin
  substring := substr(destString || sourceString || destString,
                      instr(destString || sourceString || destString,
                            destString,
                            1,
                            appearPosition) + 1,
                      instr(destString || sourceString || destString,
                            destString,
                            1,
                            appearPosition + 1) -
                      instr(destString || sourceString || destString,
                            destString,
                            1,
                            appearPosition) - 1);
  return(substring);
end substrbysep;