oracle to achieve row and column conversion (use split to remove the comma)

1. For '1','2','3','4','5' (comma is outside the string)

SQL>SELECT COLUMN_VALUE FROM TABLE(SYS.ODCIVARCHAR2LIST(‘1’,‘2’,‘3’,‘4’,‘5’));

COLUMN_VALUE

1
2
3
4
5

SQL> select regexp_substr(‘1,2,3,4,5’, ‘[^,]+’, 1, rownum) as acolumn
from dual
connect by rownum <=
length(‘1,2,3,4,5’) - length(replace(‘1,2,3,4,5’, ‘,’)) + 1;

REGEXP_SUBSTR(‘1,2,3,4,5’,’[^,

1
2
3
4
5

语句举例:
select t.orderidentity,
t.orderhiscode1,
regexp_substr(t.orderhiscode2, ‘[^;]+’, 1, LEVEL)
from (select o.orderidentity, o.orderhiscode1, o.orderhiscode2
from t_order o
where o.orderidentity = 48003313317015632) t
connect by regexp_substr(t.orderhiscode2, ‘[^;]+’, 1, LEVEL) is not null

3. Use functions

CREATE OR REPLACE TYPE ty_str_split IS TABLE OF VARCHAR2 (4000);

CREATE OR REPLACE FUNCTION fn_split (p_str IN CLOB, 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;

test:

SQL> select * from table(fn_split('1,2,3,4,5',',')); --The second single quotation mark is the character that needs to be separated in the previous string

COLUMN_VALUE -------------------------------------------------------------------------------- 1 2 3 4 5

SQL> select * from table(fn_split(‘1,2,3,4。5’,’。’));

COLUMN_VALUE -------------------------------------------------------------------------------- 1,2,3,4 5

SQL>
————————————————
Original link: https://blog.csdn.net/wanglilin/article/details/7231712

Guess you like

Origin blog.csdn.net/weixin_39597541/article/details/103128992