Oracle 自定义split 函数
Oracle没有提供split函数,但可以自己建立一个函数实现此功能。比如“abc defg hijkl nmopqr stuvw xyz”,分隔符是空格,但空格个数不定。
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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
2
3
4
5
|
1
12
123
1234
12345
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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 ;
/
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!