如何重新格式化存储在记录中的数据并将其输出到oracle中

时间:2017-05-09 07:18:25

标签: sql oracle

让我们举一个例子

我们说我的表格列中包含以下数据

   Col_ID  Col_Name
   ------  ---------------------------------------------------
   102     LOCATION_ID IN (7351,7550,76202,7350)
   121     265,76700,76701,72701,74210)
   111     ,76200,76201,76202,76203,76204,76205,76206,76207,7

上述数据以这种方式存储在已经无法更改的表中。我想要的输出如下: -

     Col_ID Col_Name
     ------ --------
        102     7531
        102     7550
        102    76202
        102     7350
        121      265
        121    76700

等等......

2 个答案:

答案 0 :(得分:0)

括号将以某种模式删除,因此请尝试以下操作并进行必要的修改。

WITH outer_table 
     AS (SELECT col_id, col_name 
           FROM (SELECT col_id, 
                        REGEXP_SUBSTR (col_name, 
                                       '[^)]+', 
                                       1, 
                                       1) 
                           col_name 
                   FROM t5 
                  WHERE col_name LIKE '%)%' 
                 UNION ALL 
                 SELECT col_id, 
                        REGEXP_SUBSTR (col_name, 
                                       '[^(]+', 
                                       1, 
                                       2) 
                           col_name 
                   FROM t5 
                  WHERE col_name LIKE '%(%' 
                 UNION ALL 
                 SELECT col_id, (col_name) col_name 
                   FROM t5 
                  WHERE col_name NOT LIKE '%(%' AND col_name NOT LIKE '%)%')) 
    select col_id, regexp_substr(col_name,'[^,]+',1,column_value) col_name 
     from  outer_table, 
           table( 
                 cast( 
                     multiset( 
                              select  level 
                                from  dual 
                                connect by level <= length(regexp_replace(col_name,'[^,]')) + 1 
                             ) 
                     as sys.OdciNumberList 
                    ) 
               ) 
  ORDER BY col_id,col_name

Demo

答案 1 :(得分:0)

WITH T AS
(SELECT '102' AS COL_ID, 'LOCATION_ID IN (7351,7550,76202,7350)' AS       COL_NAME
FROM   DUAL
UNION ALL
SELECT '121', '265,76700,76701,72701,74210)'
FROM   DUAL
UNION ALL
SELECT '111', ',76200,76201,76202,76203,76204,76205,76206,76207,7'
FROM   DUAL)
SELECT COL_ID, COL_NAME
FROM   (SELECT T.COL_ID,
            REGEXP_SUBSTR(REGEXP_SUBSTR(T.COL_NAME, '[^,]+', 1,
                                         T1.COLUMN_VALUE), '[0-9]+', 1, 1) AS COL_NAME
     FROM   T,
            TABLE(CAST(MULTISET
                        (SELECT LEVEL
                         FROM   DUAL
                         CONNECT BY LEVEL <=
                                    REGEXP_COUNT(T.COL_NAME, ',') + 1) AS
                        SYS.ODCINUMBERLIST)) T1)
WHERE  COL_NAME IS NOT NULL