当前位置:首页 > 编程笔记 > 正文
已解决

大数据-玩转数据-oracel字符串分割转化为多列

来自网友在路上 161861提问 提问时间:2023-09-19 13:03:43阅读次数: 61

最佳答案 问答题库618位专家为你答疑解惑

一、建表

create table split_string_test(id integer  primary key,test_string varchar2(500)
);

二、插入测试数据

insert into split_string_test values(1, '10,11,12,13,14,22');
insert into split_string_test values(2, '22,23,24');
insert into split_string_test values(3, '6,7,8,9');

三、语句

WITH  cntr  AS
( SELECT  LEVEL  AS lvlFROM  dualCONNECT BY  LEVEL  <= 1 +  (SELECT  MAX ( length(a.test_string) - length(replace(a.test_string, ',')) + 1) FROM  split_string_test a)                
) 
SELECT  b.id, b.test_string , lvl, REGEXP_SUBSTR( b.test_string, '([^,]+)', 1, lvl)  AS split_str
FROM  split_string_test b , cntr 
where  (lvl  <=  length(b.test_string) - length(replace(b.test_string, ',')) + 1)
and (REGEXP_SUBSTR  ( b.test_string, '([^,]+)', 1, lvl)  IS NOT NULL
OR  b.test_string  IS NULL )
ORDER BY  b.id, lvl;

注:取字符串分拆最大的数

SELECT  MAX ( length(a.test_string) - length(replace(a.test_string, ',')) + 1)  FROM  split_string_test a

或:

SELECT b.id,b.test_string,lvl,REGEXP_SUBSTR(b.test_string, '([^,]+)', 1, lvl) AS split_strFROM split_string_test b,(SELECT LEVEL AS lvlFROM dualCONNECT BY LEVEL <= 1 + (SELECT MAX(length(a.test_string) -length(replace(a.test_string, ',')) + 1)FROM split_string_test a))where (lvl <=length(b.test_string) - length(replace(b.test_string, ',')) + 1)and (REGEXP_SUBSTR(b.test_string, '([^,]+)', 1, lvl) IS NOT NULL ORb.test_string IS NULL)ORDER BY b.id, lvl;

四、结果

在这里插入图片描述

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"大数据-玩转数据-oracel字符串分割转化为多列":http://eshow365.cn/6-9328-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!