根据分隔符将oracle中的字符串拆分为多列
split a string in oracle into multiple columns based on a delimiter
需要一些关于在 oracle 中将 varchar 字段拆分为多行的帮助。
例子:
在给定的数据样本中,订单号是唯一的。我想将服务代码拆分为多行,每个服务代码由 |
分隔
data sample
谢谢!
举个例子。
示例数据在第 1 - 5 行(您的 table 中已有该数据,请勿输入);完成工作的查询从第 6 行开始。
SQL> with test (order_no, datum, service_cd) as
2 (select 17, date '2016-11-30', '2106|2100|2105' from dual union all
3 select 23, date '2016-11-30', '2043|2020|2023|2047' from dual union all
4 select 67, date '2016-11-30', null from dual
5 )
6 select order_no,
7 datum,
8 regexp_substr(service_cd, '[^|]+', 1, column_value) val
9 from test cross join
10 table(cast(multiset(select level from dual
11 connect by level <= regexp_count(service_cd, '\|') + 1
12 ) as sys.odcinumberlist))
13 order by order_no, datum, column_value;
ORDER_NO DATUM VAL
---------- ---------- ----------
17 30/11/2016 2106
17 30/11/2016 2100
17 30/11/2016 2105
23 30/11/2016 2043
23 30/11/2016 2020
23 30/11/2016 2023
23 30/11/2016 2047
67 30/11/2016
8 rows selected.
SQL>
需要一些关于在 oracle 中将 varchar 字段拆分为多行的帮助。 例子: 在给定的数据样本中,订单号是唯一的。我想将服务代码拆分为多行,每个服务代码由 |
分隔data sample
谢谢!
举个例子。
示例数据在第 1 - 5 行(您的 table 中已有该数据,请勿输入);完成工作的查询从第 6 行开始。
SQL> with test (order_no, datum, service_cd) as
2 (select 17, date '2016-11-30', '2106|2100|2105' from dual union all
3 select 23, date '2016-11-30', '2043|2020|2023|2047' from dual union all
4 select 67, date '2016-11-30', null from dual
5 )
6 select order_no,
7 datum,
8 regexp_substr(service_cd, '[^|]+', 1, column_value) val
9 from test cross join
10 table(cast(multiset(select level from dual
11 connect by level <= regexp_count(service_cd, '\|') + 1
12 ) as sys.odcinumberlist))
13 order by order_no, datum, column_value;
ORDER_NO DATUM VAL
---------- ---------- ----------
17 30/11/2016 2106
17 30/11/2016 2100
17 30/11/2016 2105
23 30/11/2016 2043
23 30/11/2016 2020
23 30/11/2016 2023
23 30/11/2016 2047
67 30/11/2016
8 rows selected.
SQL>