2013-03-13 99 views
0

我與這些值的表列:在許多列拆分單詞的sql

Column-desc 
------------------ 
"car broken" 
"motorcycle broken" 
"motorcycle clean" 
"motorcycle clean and broken" 
------------------ 

,我需要做一個選擇,返回此:

return-desc1 | return-desc2 | return-desc3 |return-desc4 
------------------------------------------------------ 
car   | broken  |    | 
motorcycle | broken  |    | 
motorcycle | clean  |    | 
motorcycle | clean  | and   | broken 
------------------------------------------------------- 

(是的,最多會返回4 DESC)。 我可以在這種情況下使用正則表達式嗎?

如果這可以幫助:我使用Oracle 10g

+1

這可能讓你在正確的方向前進:http://stackoverflow.com/questions/5123585/how-to-split-a-single-column-values-to-multiple-column-values – 2013-03-13 13:50:05

+0

很酷,它會幫助我,我可以一步一步來。 – rcorbellini 2013-03-13 13:51:22

+0

它不工作在oracle = [ – rcorbellini 2013-03-13 13:55:21

回答

3
select 
    regexp_substr("Column-desc", '\S+', 1, 1) as "return-desc1", 
    regexp_substr("Column-desc", '\S+', 1, 2) as "return-desc2", 
    regexp_substr("Column-desc", '\S+', 1, 3) as "return-desc3", 
    regexp_substr("Column-desc", '\S+', 1, 4) as "return-desc4" 
from your_table 
+0

完美這是一個完美的答案! – rcorbellini 2013-03-13 13:58:06

0

老實說,我的建議是讓列遞減,做編程層分割工作。這樣做會更容易,可能更適合。