2013-03-21 65 views
0

我有这样分割字符串根据分隔符在Oracle

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 

帮我输出了如何使用循环使用子和INSTR串得到输出

感谢和问候 阿南德

+1

你尝试过什么?你有没有试图写查询,你有任何错误? – 2013-03-21 13:54:48

+0

所以你想要octothorp和字符串数量之间的一切? – 2013-03-21 14:03:04

+0

哇,对不起,你必须处理这样的表格。真的应该有说明表正常化。也许你可以使用流水线功能和视图?看看管道功能示例-http://www.akadia.com/services/ora_pipe_functions.html – OldProgrammer 2013-03-21 14:05:14

回答

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

+0

整洁的方法,我想提出一种分离技术 – Sebas 2013-03-21 14:19:26