2

複数のフィールドを連結する必要がありますが、null である場合とそうでない場合があります。',,c,,e,,' のような文字列になる可能性がありますが、これは実際には 'c,e' として表示したいものです。

regexp_replaceとの組み合わせでこれを取得できますtrim

with sd as (select 'a,b,c' str from dual union all
            select 'a' str from dual union all
            select null str from dual union all
            select 'a,,,d' from dual union all
            select 'a,,,,e,f,,'from dual union all
            select ',,,d,,f,g,,'from dual)
select str,
       regexp_replace(str, '(,)+', '\1') new_str,
       trim(both ',' from regexp_replace(str, '(,)+', '\1')) trimmed_new_str
from   sd;

STR         NEW_STR     TRIMMED_NEW_STR
----------- ----------- ---------------
a,b,c       a,b,c       a,b,c          
a           a           a              

a,,,d       a,d         a,d            
a,,,,e,f,,  a,e,f,      a,e,f          
,,,d,,f,g,, ,d,f,g,     d,f,g  

regexp_replaceしかし、私はそれが1回だけで実行可能であるべきだと感じています.

出来ますか?もしそうなら、どのように?

4

2 に答える 2

5

クエリ:

with sd as (select 'a,b,c' str from dual union all
            select 'a' from dual  union all
            select null from dual union all
            select 'a,,,d,' from dual  union all
            select ',a,,,d' from dual  union all
            select ',a,,,d,' from dual  union all
            select ',,,a,,,d,,,' from dual  union all
            select ',a,,,,,e,f,,' from dual union all
            select ',,d,,f,g,,' from dual )
select str,
       regexp_replace(str, '^,+|,+$|,+(,\w)','\1') new_str
from   sd;

結果:

str             new_str
-----------------------
a,b,c           a,b,c
a               a
(null)          (null)  
a,,,d,          a,d
,a,,,d          a,d
,a,,,d,         a,d
,,,a,,,d,,,     a,d
,a,,,,,e,f,,    a,e,f
,,d,,f,g,,      d,f,g

パターン:

  ^,+       matches commas at the beginning
  |         OR
  ,+$       matches commas at the end
  |         OR
  ,+(,\w)   matches several commas followed by a single comma and a word.

上記をコンマと単語である最初のサブ式のみに置き換えます。

于 2016-02-09T06:53:14.053 に答える
-1

試してみる:

with sd as (select 'a,b,c' str union all
            select 'a' union all
            select null union all
            select 'a,,,d' union all
            select 'a,,,,,e,f,,' union all
            select ',,,d,,f,g,,')
select str,
       regexp_replace(str, '(,){2,}', '\1', 1, 0) new_str,
       trim(both ',' from regexp_replace(str, '(,){2,}', '\1', 1, 0)) trimmed_new_str
from   sd;
于 2016-02-08T18:23:45.610 に答える