1

我想保留以逗号分隔的字符串形式提供的记录顺序。由分隔的字符串中的第 5项为空。我也需要第 5行为空。

 with test as 
(select 'ABC,DEF,GHI,JKL,,MNO' str from dual  
  )  
  select rownum, regexp_substr (str, '[^,]+', 1, rownum) split  
    from test  
 connect by  level <= length (regexp_replace (str, '[^,]+' ))  + 1

我得到的当前结果将其置于第 6

1   ABC
2   DEF
3   GHI
4   JKL
5   MNO
6   
4

3 回答 3

3

您的表达式保留了顺序,但您的正则表达式未正确匹配空值,因此第 5 项消失了。第 6 行是 a NULL,因为在第 5 场比赛之后没有更多比赛。

你可以这样做:

SQL> with test as
  2  (select 'ABC,DEF,GHI,JKL,,MNO' str from dual
  3    )
  4  SELECT rownum,
  5         rtrim(regexp_substr(str || ',', '[^,]*,', 1, rownum), ',') split
  6    FROM test
  7  CONNECT BY LEVEL <= length(regexp_replace(str, '[^,]+')) + 1;

    ROWNUM SPLIT
---------- ---------------------------------------------------------------
         1 ABC
         2 DEF
         3 GHI
         4 JKL
         5 
         6 MNO

6 rows selected

或这个:

SQL> with test as
  2  (select 'ABC,DEF,GHI,JKL,,MNO' str from dual
  3    )
  4  SELECT rownum,
  5         regexp_substr(str, '([^,]*)(,|$)', 1, rownum, 'i', 1) split
  6    FROM test
  7  CONNECT BY LEVEL <= length(regexp_replace(str, '[^,]+')) + 1;

    ROWNUM SPLIT
---------- ------------------------------------------------------------
         1 ABC
         2 DEF
         3 GHI
         4 JKL
         5 
         6 MNO

6 rows selected
于 2013-09-17T15:30:22.803 回答
0

尝试这样的事情:

SELECT
      STR,
      REPLACE ( SUBSTR ( STR,
                     CASE LEVEL
                         WHEN 1
                         THEN
                             0
                         ELSE
                             INSTR ( STR,
                                    '~',
                                    1,
                                    LEVEL
                                    - 1 )
                     END
                     + 1,
                     1 ),
              '~' )
FROM
      (SELECT 'A~~C~~E' AS STR FROM DUAL)
CONNECT BY
      LEVEL <= LENGTH ( REGEXP_REPLACE ( STR,
                                  '[^~]+' ) )
             + 1;
于 2013-09-17T15:38:39.513 回答
0

这个有效..

SELECT
      ROWNUM,
      CAST ( REGEXP_SUBSTR ( STR,
                        '(.*?)(,|$)',
                        1,
                        LEVEL,
                        NULL,
                        1 ) AS CHAR ( 12 ) )
          OUTPUT
FROM
      (SELECT 'ABC,DEF,GHI,JKL,,MNO' AS STR FROM DUAL)
CONNECT BY
      LEVEL <= REGEXP_COUNT ( STR,
                         ',' )
             + 1;
于 2013-09-17T15:43:10.207 回答