提取某些字符串并将它们分发到相邻列

时间:2013-07-30 15:57:41

标签: sql regex plsql oracle11gr2

我有一张类似的表:

with tab as(
select 'P710345,P345123 are not valid' m from dual
union all
select 'P901236,P234098,P675001 are not valid' m from dual)
select * from tab

我需要的是提取以P开头的字符串,并将这些字符串放在同一行的新列中。

最终结果:

P710345,P345123 are not valid         | P710345 | P345123 |        |
P901236,P234098,P675001 are not valid | P901236 | P234098 |P675001 |

我尝试使用regexp_substr提取:

with tab as(
select 'P710345,P345123 are not valid' m from dual
union all
select 'P901236,P234098,P675001 are not valid' m from dual )
select regexp_substr (m,'P\d\w+','2') b from tab

我现在被困在这里。

1 个答案:

答案 0 :(得分:0)

我找到的最简单的解决方法是制作一个csv然后导入它或在速度表程序中使用它

SELECT CONCAT(
  "'", m, "';'", 
  REPLACE(
     SUBSTR(m, 1, POSITION(' ' IN m)), 
     ',', 
     "';'"), 
  "';") 
FROM ( SELECT 'P710345,P345123 are not valid' AS m FROM DUAL UNION ALL SELECT 'P901236,P234098,P675001 are not valid' AS m FROM DUAL) AS t;