Oracleデータベース正則による文字列の切断

8130 ワード

1、方法一:
SELECT regexp_substr('161,83,66,81','[0-9]+',1,LEVEL) FROM dual
CONNECT BY  level <= length('161,83,66,81') - length(regexp_replace('161,83,66,81',',','')) + 1;

追加のプロセスバックアップは次のとおりです(最後のセミコロンがプロセスに必要かどうか注意してください).
SELECT ID ,DEPARTMENTID ,LASTNAME  FROM HRMRESOURCE WHERE LOGINID IS NOT NULL AND DEPARTMENTID IN
(SELECT regexp_substr(t.deptid,'[0-9]+',1,LEVEL) FROM (SELECT (CASE to_char(DEPARTMENTID) WHEN to_char(161)
THEN 161||','||81||','||66||','||83 ELSE to_char(DEPARTMENTID) END ) deptid FROM HRMRESOURCE WHERE DEPARTMENTID = 161) t
CONNECT BY  level <= length(t.deptid) - length(regexp_replace(t.deptid,',','')) + 1) AND LASTNAME NOT LIKE '% %';

2、方法2:
CREATE OR REPLACE TYPE SPLIT_STR AS TABLE OF VARCHAR2(100);

CREATE OR REPLACE FUNCTION split_strs(str VARCHAR2 ,reg VARCHAR2)
RETURN SPLIT_STR
  PIPELINED  IS
  V_LENGTH NUMBER := LENGTH(str);
  V_START NUMBER := 1;
  V_INDEX NUMBER;
  BEGIN
    WHILE V_START <= V_LENGTH LOOP
      V_INDEX := INSTR(str, reg, V_START);
      IF V_INDEX = 0 THEN
      PIPE ROW(SUBSTR(str, V_START));
      V_START := V_LENGTH + 1;
      ELSE
      PIPE ROW(SUBSTR(str, V_START, V_INDEX - V_START));
      V_START := V_INDEX + 1;
      END IF;
    END LOOP;
    RETURN ;
  END;

SELECT * FROM TABLE (split_strs('1,2,3,4,5',','));