2017-08-16 63 views
1

我想串聯一個列的值,但這些值必須格式化爲另一個字符串。連接列值

這裏是我的表:

C1   C2      c3     c4 
--------- ---------    ------    -------- 
ID1   28-OCT-16 11.59.00  (null)    04-OCT-16 08.48.00 
ID2   (null)     05-OCT-16 02.55.00 (null) 
ID3   (null)     10-OCT-16 04.32.00 21-OCT-16 02.25.00 
ID4   10-OCT-16 04.32.00  18-OCT-16 08.52.00 18-OCT-16 08.32.00 
ID5   10-OCT-16 04.32.00  (null)    (null) 

我已經完成格式化表格來匹配我需要的價值。

select 
    c1 T_ID, 
    case when c2 is not null then 'Plane' end PLANE, 
    case when c3 is not null then 'BUS' end BUS, 
    case when c4 is not null then 'Hotel' end HOTEL 
    from table1 
order by 1; 




T_ID  PLANE     BUS     HOTEL 
--------- ---------    ------    -------- 
ID1   Plane     (null)    Hotel 
ID2   (null)     BUS     (null) 
ID3   (null)     BUS     Hotel 
ID4   Plane     BUS     Hotel 
ID5   Plane     (null)    (null) 

而且我嘗試做以下

T_ID  SERVICE   
--------- ---------  
ID1   Plane+Hotel   
ID2   BUS   
ID3   BUS+Hotel   
ID4   Plane+BUS+Hotel   
ID5   Plane 

我試過一對夫婦級聯功能,但無法找到我要找的結果。

+0

「我試過幾個串接函數」 - 你有什麼試過?什麼不行? –

+0

向我們展示你在找什麼結果請 –

回答

1

基本上可以做到:

select c1 T_ID, 
     substr((case when c2 is not null then '+Plane' end) || 
       (case when c3 is not null then '+BUS' end) || 
       (case when c4 is not null then '+Hotel' end) 
       2) 
from table1 
order by 1; 

這基本上通過將分離器在字符串中的每個組件的開始實現功能concat_ws()。外部substr()刪除第一個字符。

+0

是的,我喜歡那個....讓我試試看.. – Zombraz