基于oracle中的分隔符拆分字符串

时间:2013-03-21 13:50:22

标签: sql oracle10g

我有一张这样的表

name Description
VM1  SP L A - WINSVRS #P19  QTY 1
VM2  SPLA - VRSTD #P29-9 9 99 QTY 2 : SPLVRENT #P3 999999 9 QTY 3
VM3  SPLA - WINSVRSTD #P39-9 999 QTY 3
VM4  SPLA - WI NS V R STD #P59- 9 9 99 QTY 2 : S P LA - W IN SV RENT #P39-9999 QTY 3 :   SPLA - WIN S VR SMB # P 3 9- 999 99 QTY 5
VM6  SPLA - WINSVRSQLSERVERaaaaaaaaaSTD #P 6 9-9 9 9 9 QTY 6

我需要这样的输出

name Description                     ponumber
-------------------------------------------------
VM1  SP L A - WINSVRS                   P19 
VM2  SPLA - VRSTD                       P29-9 9 99                      
VM2  SPLVRENT                           P3 999999 9
VM3  SPLA - WINSVRSTD                   P39-9 999
VM4  SPLA - WI NS V R STD               P59- 9 9 99
VM4  S P LA - W IN SV RENT              P39-9999
VM4  SPL A - WIN S VR SMB               P 3 9- 999 99
VM6  SPLA - WINSVRSQLSERVERaaaaaaaaaSTD P 6 9-9 9 9 9

帮助我如何使用循环

使用substring和instr字符串获取输出

感谢和问候 阿南德

1 个答案:

答案 0 :(得分:2)

试试这个

with CTE as
(
  select name, 
  trim(cast(v.column_value.extract('//text()') as varchar2(50))) description
  from t,
  table( xmlsequence( xmltype(
    '<descriptions><description>' 
     || replace(description, ':', '</description><description>') 
     || '</description></descriptions>'
        ).extract('//descriptions/*'))) v
 )
select 
 name,
 REGEXP_SUBSTR(description,'[^#]+') description,
 --REGEXP_SUBSTR(description,'[^#]+',1,2) phnQty,
 Trim(REGEXP_SUBSTR(REGEXP_SUBSTR(description,'[^#]+',1,2),'[^Q]+')) phn
from cte;

SQL DEMO