将逗号分隔为Oracle中的列返回值时使用255个逗号分隔的值。是否有一种不需要255个Substr就可以将这些列分割成列的简单方法?ROW | VAL----------- 1 | 1.25, 3.87, 2, ... 2 | 5, 4, 3.3, ....到ROW | VAL | VAL | VAL ...--------------------- 1 |1.25 |3.87 | 2 ... 2 | 5 | 4 | 3.3 ...
3 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
regexp_substr()
:
select regexp_substr(val, '[^,]+', 1, 1) as val1, regexp_substr(val, '[^,]+', 1, 2) as val2, regexp_substr(val, '[^,]+', 1, 3) as val3, . . .
慕神8447489
TA贡献1780条经验 获得超1个赞
'[^,]+'
SQL> select regexp_substr('1,2,3,,5,6', '[^,]+', 1, 5) from dual;R-6
SQL> select regexp_substr('1,2,3,,5,6', '(.*?)(,|$)', 1, 5, NULL, 1) from dual;R-5
'(.*?)(,|$)'
( = Start a group. = match any character* = 0 or more matches of the preceding character? = Match 0 or 1 occurrences of the preceding pattern) = End the 1st group( = Start a new group (also used for logical OR), = comma| = OR$ = End of the line) = End the 2nd group
'[^,]+'
'[^,]+'
!
汪汪一只猫
TA贡献1898条经验 获得超8个赞
创建您自己的内置 cto_table
函数在任何分隔符上拆分字符串。 ,然后你可以使用 PIVOT + LISTAGG
这样做如下:
select * from ( select rownum r , collection.* from TABLE(cto_table(',','1.25, 3.87, 2, 19,, 1, 9, ')) collection)PIVOT ( LISTAGG(column_value) within group (order by 1) as val for r in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
FYI:cto_table
CREATE OR REPLACE TYPE t_my_list AS TABLE OF VARCHAR2(100);CREATE OR REPLACEFUNCTION cto_table(p_sep in Varchar2, p_list IN VARCHAR2) RETURN t_my_listAS l_string VARCHAR2(32767) := p_list || p_sep; l_sep_index PLS_INTEGER; l_index PLS_INTEGER := 1; l_tab t_my_list := t_my_list();BEGIN LOOP l_sep_index := INSTR(l_string, p_sep, l_index); EXIT WHEN l_sep_index = 0; l_tab.EXTEND; l_tab(l_tab.COUNT) := TRIM(SUBSTR(l_string,l_index,l_sep_index - l_index)); l_index := l_sep_index + 1; END LOOP; RETURN l_tab;END cto_table;/
添加回答
举报
0/150
提交
取消