Oracle 自訂split 函數
Oracle沒有提供split函數,但可以自行建立一個函數來實現此功能。例如“abc defg hijkl nmopqr stuvw xyz”,分隔符號為空格,但空格數不定。
原始碼:
CREATE OR REPLACE TYPE ty_str_split IS TABLE OF VARCHAR2 (4000); CREATE OR REPLACE FUNCTION fn_var_split ( p_str IN VARCHAR2, p_delimiter IN VARCHAR2 ) RETURN ty_str_split IS j INT := 0; len INT := 0; str VARCHAR2 (4000); str_split ty_str_split := ty_str_split (); v_str VARCHAR2 (4000) := RTRIM (LTRIM (p_str, p_delimiter), p_delimiter); BEGIN len := LENGTH (v_str); WHILE len > 0 LOOP j := INSTR (v_str, p_delimiter, 1); IF j = 0 THEN str := SUBSTR (v_str, 1); len := 0; str_split.EXTEND; str_split (str_split.COUNT) := str; ELSE str := SUBSTR (v_str, 1, j - 1); v_str := LTRIM (LTRIM (v_str, str), p_delimiter); len := LENGTH (v_str); str_split.EXTEND; str_split (str_split.COUNT) := str; END IF; END LOOP; RETURN str_split; END fn_var_split; /
測驗:
結果:
1 12 123 1234 12345
DECLARE CURSOR c IS SELECT * FROM TABLE (CAST (fn_var_split (';1;12;;123;;;1234;;;;12345;', ';') AS ty_str_split)); r c%ROWTYPE; BEGIN OPEN c; LOOP FETCH c INTO r; EXIT WHEN c%NOTFOUND; DBMS_OUTPUT.put_line (r.column_value); END LOOP; CLOSE c; END; /
感謝閱讀,希望能幫助大家,謝謝大家對本站的支持!
更多Oracle 自訂split 函數實例詳解相關文章請關注PHP中文網!