DenisKozlov
2/27/2018 - 10:13 AM

Split comma-separated string (PIPELINED function)

use as: SELECT * FROM TABLE(split_string('1,2,3'))

  TYPE varchar_tab IS TABLE OF VARCHAR2(100);

  FUNCTION split_string(in_comma_separated_values IN VARCHAR2) RETURN varchar_tab
    PIPELINED IS
    val    VARCHAR2(32);
    i_from PLS_INTEGER := 1;
    i_to   PLS_INTEGER;
    len    PLS_INTEGER;
  BEGIN
    len := LENGTH(in_comma_separated_values);
    LOOP
      i_to := instr(in_comma_separated_values, ',', i_from, 1);
      IF i_to = 0
      THEN
        i_to := len + 1;
      END IF;
      val := substr(in_comma_separated_values, i_from, i_to - i_from);
      PIPE ROW(val);
      IF i_to >= len
      THEN
        RETURN;
      END IF;
      i_from := i_to + 1;
    END LOOP;
  END;