Oracle split string of segmentation function

Oracle string split, split function strings.

The first: oracle string Segmentation and

Split

Copy the code
create or replace function Get_StrArrayLength
(
  av_str varchar2, - the string to be split
  av_split varchar2 - delimiter
)
return number
is
  lv_str varchar2 (1000);
  lv_length number;
begin
  lv_str = ltrim (rtrim (av_str));
  lv_length:=0;
  while instr(lv_str,av_split)<>0 loop
     lv_length:=lv_length+1;
     lv_str = substr (lv_str, instr (lv_str, av_split) + length (av_split), length (lv_str));
  end loop;
  lv_length:=lv_length+1;
  return lv_length;
end Get_StrArrayLength;
Copy the code

 

extract

 

Copy the code
create or replace function Get_StrArrayStrOfIndex
(
  av_str varchar2, - the string to be split
  av_split varchar2, - delimiter
  av_index number - several elements take the first
)
return varchar2
is
  lv_str varchar2 (1024);
  lv_strOfIndex varchar2 (1024);
  lv_length number;
begin
  lv_str = ltrim (rtrim (av_str));
  lv_str = concat (lv_str, av_split);
  lv_length = av_index;
  if lv_length=0 then
      lv_strOfIndex = substr (lv_str, 1, instr (lv_str, av_split) -length (av_split));
  else
      lv_length = av_index + 1;
     lv_strOfIndex = substr (lv_str, instr (lv_str, av_split, 1, av_index) + length (av_split), instr (lv_str, av_split, 1, lv_length) -instr (lv_str, av_split, 1, av_index) -length (av_split)) ;
  end if;
  return lv_strOfIndex;
end Get_StrArrayStrOfIndex;
Copy the code

测试:   select Get_StrArrayStrOfIndex('songguojun$@111111537','$',0) from dual

result:


 

The second:

This function can be the "target character string" to "specify the string" split, and returns the results table structure. code show as below:

Copy the code
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;
Copy the code

Once created, we have to test, for example, execute the following SQL:

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

Its output is a table of two rows, as shown below:

2009-9-9-11.38.15

Rows into columns show:

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

Figure:

2009-9-9-11.44.53

Reproduced in: https: //www.cnblogs.com/zhichao-116/p/3645600.html

Guess you like

Origin blog.csdn.net/weixin_33724570/article/details/93356246