2013-03-21 63 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