-
SQL - String To SplitDB/SQL 2023. 8. 7. 15:04반응형
문자열과 구분자를 받아서 Split 하는 Function
Split 이후 IN 조건으로 활용
-- 변수부터 진행CREATE OR REPLACE TYPE ESHOP.T_VARCHAR2 AS TABLE OF VARCHAR2(32767);-- FUNC 생성CREATE OR REPLACE FUNCTION ESHOP.F_STRING_TO_SPLIT (P_STR IN VARCHAR2,P_SEP IN VARCHAR2 DEFAULT ',' )RETURN T_VARCHAR2ISL_RESULT T_VARCHAR2 := T_VARCHAR2();L_STR_LENGTH PLS_INTEGER;L_SEP_LENGTH PLS_INTEGER;L_SEP_TYPE BOOLEAN;L_START PLS_INTEGER NOT NULL := 1;L_FOUND PLS_INTEGER;L_COUNT PLS_INTEGER NOT NULL := 1;L_SEP VARCHAR2(1);BEGINL_SEP := SUBSTR(P_SEP, 1, 1);IF P_STR IS NULL THEN RETURN L_RESULT; END IF;
L_STR_LENGTH := LENGTH(P_STR);
IF L_SEP IS NULL THEN
FOR I IN 1 .. LEAST(L_STR_LENGTH, L_STR_LENGTH) LOOPL_RESULT.EXTEND;L_RESULT(L_RESULT.COUNT) := SUBSTR(P_STR, I, 1);END LOOP;ELSE
L_SEP_LENGTH := LENGTH(L_SEP);L_SEP_TYPE := L_SEP_LENGTH > 1;
LOOP
IF L_SEP_TYPE THENL_FOUND := REGEXP_INSTR(P_STR, L_SEP, L_START);L_SEP_LENGTH := NVL(LENGTH(REGEXP_SUBSTR(P_STR, L_SEP, L_START)), 0);ELSEL_FOUND := INSTR(P_STR, L_SEP, L_START);END IF;
IF L_FOUND > 0 THENL_RESULT.EXTEND;L_RESULT(L_COUNT) := SUBSTR(P_STR, L_START, L_FOUND-L_START);L_START := L_FOUND + L_SEP_LENGTH;ELSEL_RESULT.EXTEND;L_RESULT(L_COUNT) := SUBSTR(P_STR, L_START, L_STR_LENGTH-L_START+1);EXIT;END IF;
L_COUNT := L_COUNT + 1;END LOOP;END IF;
RETURN L_RESULT;END F_STRING_TO_SPLIT;/반응형'DB > SQL' 카테고리의 다른 글
Update Query시 두 테이블 간 1:1 대칭 후 값 Set 시키기 - Merge Into (0) 2019.03.28 Eclipse - SQL Explorer 실행 (0) 2018.08.26 SQL Study3 - SELECT (0) 2018.08.07 SQL Study2 - SELECT (0) 2018.08.06 SQL SELECT Study (0) 2018.08.03 댓글