2017-06-29 50 views
0

我有一個字符串425874761-G#463733361-S#48310-S。我需要一個SQL將這個字符串變成如下的列。在oracle中解析字符串

Column_1  Column_2 
425874761   G 
463733361   S 
48310    S 

如果輸入48310-S#425874761-G然後輸出應該

Column_1  Column_2 
48310    S 
425874761   G 

感謝提前的幫助!

回答

0

您可以使用此:

WITH temp AS (
    SELECT '425874761-G #463733361-S #48310-S' col1 FROM dual 
) 
SELECT 
    --REGEXP_SUBSTR(t.col1, '[^#]+', 1, levels.column_value) AS split_rows, 
    REGEXP_SUBSTR(REGEXP_SUBSTR(t.col1, '[^#]+', 1, levels.column_value), '[^-]+', 1, 1) AS col1, 
    REGEXP_SUBSTR(REGEXP_SUBSTR(t.col1, '[^#]+', 1, levels.column_value), '[^-]+', 1, 2) AS col2 
FROM 
    temp t, 
    TABLE(CAST(MULTISET(SELECT level FROM dual CONNECT BY level <= LENGTH (REGEXP_REPLACE(t.col1, '[^#]+')) + 1) AS sys.OdciNumberList)) levels; 

這個答案從string_to_rowsstring_to_columns

參考解決方案