/* 1. 오라클 정규표현식함수을 이용한 문자열 분리 2. 오라클 문자열 관련 single-row function을 이용한 문자열 분리 2019.03.12, Jun H. Lee */ SELECT COL1 , REGEXP_SUBSTR(COL1, '([^'||DELIMITER||']*)(\'||DELIMITER||'|$)', 1, 1, NULL, 1) AS REGEXP_SPLIT1 , REGEXP_SUBSTR(COL1, '([^'||DELIMITER||']*)(\'||DELIMITER||'|$)', 1, 2, NULL, 1) AS REGEXP_SPLIT2 , REGEXP_SUBSTR(COL1, '([^'||DELIMITER||']*)(\'||DELIMITER||'|$)', 1, 3, NULL, 1) AS REGEXP_SPLIT3 , REGEXP_SUBSTR(COL1, '([^'||DELIMITER||']*)(\'||DELIMITER||'|$)', 1, 4, NULL, 1) AS REGEXP_SPLIT4 , REGEXP_SUBSTR(COL1, '([^'||DELIMITER||']*)(\'||DELIMITER||'|$)', 1, 5, NULL, 1) AS REGEXP_SPLIT5 , SUBSTR(COL1, 1, INSTR(COL1, DELIMITER, 1, 1)-1) AS FUNCION_SPLIT1 , SUBSTR(COL1, INSTR(COL1, DELIMITER, 1, 1)+1, INSTR(COL1, DELIMITER, 1, 2)-INSTR(COL1, DELIMITER, 1, 1)-1) AS FUNCION_SPLIT2 , SUBSTR(COL1, INSTR(COL1, DELIMITER, 1, 2)+1, INSTR(COL1, DELIMITER, 1, 3)-INSTR(COL1, DELIMITER, 1, 2)-1) AS FUNCION_SPLIT3 , SUBSTR(COL1, INSTR(COL1, DELIMITER, 1, 3)+1, INSTR(COL1, DELIMITER, 1, 4)-INSTR(COL1, DELIMITER, 1, 3)-1) AS FUNCION_SPLIT4 , SUBSTR(COL1, INSTR(COL1, DELIMITER, 1, 4)+1, LENGTH(COL1)-INSTR(COL1, DELIMITER, 1, 4)) AS FUNCION_SPLIT5 FROM (SELECT 'TEST1 TEST2 TEST3 TEST4 TEST5 ' AS COL1 , CHR(10) AS DELIMITER FROM DUAL ) ;
WITH t AS ( SELECT 'HELLOW ORACLE TEST ' col1 FROM dual UNION ALL SELECT 'HELLOW TEST ' col1 FROM dual ) SELECT col1 , REGEXP_SUBSTR(col1, '.+', 1, 1) split1 , REGEXP_SUBSTR(col1, '.+', 1, 2) split2 , REGEXP_SUBSTR(col1, '.+', 1, 3) split3 , REGEXP_SUBSTR(col1, '.+', 1, 4) split4 FROM t ;
ETL등 실무 적용 시, 분리된 문자열이 NULL인 경우에 대한 대비가 필요합니다.
아닐 경우, 필드값이 앞으로 밀리는 결과가 발생할 수 있습니다.