/** * 字符串分割函数. * @param P_STR 待分割的字符串 * @param 分隔符 * @return 自定义table类型TY_STR_SPLIT. * 使用方法 select column_value from table(fn_split('1,2',',')) * @Author: xDer */ CREATE OR REPLACE FUNCTION FN_SPLIT(P_STR IN VARCHAR2, P_DELIMITER IN VARCHAR2) RETURN TY_STR_SPLIT IS J INT := 0; I INT := 1; LEN INT := 0; LEN1 INT := 0; STR VARCHAR2(4000); STR_SPLIT TY_STR_SPLIT := TY_STR_SPLIT(); BEGIN LEN := LENGTH(P_STR); LEN1 := LENGTH(P_DELIMITER); WHILE J < LEN LOOP J := INSTR(P_STR, P_DELIMITER, I); IF J = 0 THEN J := LEN; STR := SUBSTR(P_STR, I); STR_SPLIT.EXTEND; STR_SPLIT(STR_SPLIT.COUNT) := STR; IF I >= LEN THEN EXIT; END IF; ELSE STR := SUBSTR(P_STR, I, J - I); I := J + LEN1; STR_SPLIT.EXTEND; STR_SPLIT(STR_SPLIT.COUNT) := STR; END IF; END LOOP; RETURN STR_SPLIT; END FN_SPLIT;
/** * 字符串分割函数. * @param P_STR 待分割的字符串 * @param 分隔符 * @return 自定义table类型TY_STR_SPLIT. * 使用方法 select column_value from table(fn_split('1,2',',')) * @Author: xDer */CREATE OR REPLACE FUNCTION FN_SPLIT(P_STR IN VARCHAR2, P_DELIMITER IN VARCHAR2) RETURN TY_STR_SPLIT IS J INT := 0; I INT := 1; LEN INT := 0; LEN1 INT := 0; STR VARCHAR2(4000); STR_SPLIT TY_STR_SPLIT := TY_STR_SPLIT();BEGIN LEN := LENGTH(P_STR); LEN1 := LENGTH(P_DELIMITER);
WHILE J < LEN LOOP J := INSTR(P_STR, P_DELIMITER, I);
IF J = 0 THEN J := LEN; STR := SUBSTR(P_STR, I); STR_SPLIT.EXTEND; STR_SPLIT(STR_SPLIT.COUNT) := STR;
IF I >= LEN THEN EXIT; END IF; ELSE STR := SUBSTR(P_STR, I, J - I); I := J + LEN1; STR_SPLIT.EXTEND; STR_SPLIT(STR_SPLIT.COUNT) := STR; END IF; END LOOP;
RETURN STR_SPLIT;END FN_SPLIT;