Oracle 自定义split 函数实例详解
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; /
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关推荐
LuoXinLoves 2019-12-24
银角大王 2020-06-25
Hhanwen 2020-05-12
zhangll00 2020-05-07
sui 2020-04-29
GeorgeTH 2020-03-02
Aggressivesnail 2020-02-28
laohyx 2020-01-30
清风徐来水波不兴 2020-01-05
sdwylry 2020-01-05
budding0 2019-12-17
iphilo 2019-12-16
jiangtie 2019-12-09
嗡汤圆 2019-11-10
LiHansiyuan 2019-11-03
程松 2019-09-05
上方科技技术交流 2016-07-11