在oracle 对有效的字符串分割,后并获得每个分割的字符

---例如: segmet1,segmet2,segmet3,segmet4

 

---p_string =‘segmet1,segmet2,segmet3,segmet4’

---p_element 它是对应的第几个字符

---p_separator 对应的分割符比如:‘,’

 

  PROCEDURE gen_test1( p_string IN VARCHAR2
                      ,p_element IN INTEGER
                      ,p_separator IN VARCHAR2) IS
   
    v_string           VARCHAR2(4000);
    v_string2  VARCHAR2(4000);
  BEGIN

    v_string := p_string || p_separator;
    FOR i IN 1 .. p_element - 1
    LOOP
      v_string := substr(v_string, instr(v_string, p_separator) + 1);
      dbms_output.put_line(v_string);
    END LOOP;
    v_string2 :=substr(v_string, 1, instr(v_string, p_separator) - 1);
   dbms_output.put_line(v_string2);
                       
  END gen_test1;

-当p_element为1时就可以返回segmet1

--当p_element为2时就可以返回segmet2

--当p_element为3时就可以返回segmet3

--当p_element为4时就可以返回segmet4

--------------------------------------------------------------

 

--包头:


  FUNCTION get_element(p_string    VARCHAR2
                      ,p_element   INTEGER
                      ,p_separator VARCHAR2) RETURN VARCHAR2;

---函数方法:

  FUNCTION get_element(p_string    VARCHAR2
                      ,p_element   INTEGER
                      ,p_separator VARCHAR2) RETURN VARCHAR2 AS
    v_string VARCHAR2(32767);
  BEGIN
    v_string := p_string || p_separator;
    FOR i IN 1 .. p_element - 1
    LOOP
      v_string := substr(v_string, instr(v_string, p_separator) + 1);
    END LOOP;
    RETURN substr(v_string, 1, instr(v_string, p_separator) - 1);
  END get_element;

你可能感兴趣的:(在oracle 对有效的字符串分割,后并获得每个分割的字符)