根据大写和小写拆分列

时间:2017-12-11 10:07:46

标签: sql oracle

我有一个表格,列中的值类似于:

A

FOO          Foo

DOE          John 

UPPER        Lower

VAN MECKELEN Maria

我想把它分成两列,其中完全上面的字符串放在第一列中,字符串放在另一列上

B            |       C
-------------------------
FOO          |Foo

DOE          |John

UPPER        |Lower

VAN MECKELEN |Maria

有没有简单的方法呢?

2 个答案:

答案 0 :(得分:1)

我刚想通了: 查询是,

Select
  REGEXP_SUBSTR(A, '[[:upper:]]+[[:space:]]{0,1}[[:upper:]]+') as B,
  REGEXP_SUBSTR(A, '[[:upper:]][[:lower:]]+') as C,
From MY_TABLE

答案 1 :(得分:1)

正如我上面提到的,您提供的上述正则表达式不适用于所有类型的输入。因此,通过几个不同的输入进行测试,相应地改变了正则表达式。 如果您需要,可以使用以下查询。

with t as (
        select 'FOO Foo' as coln from dual
        union 
        select 'UPPER Lower' from dual
        union
        select 'VAN MECKELEN EFGD Mari' from dual
        union
        select 'ABCE EFG DFC' from dual
        union
        select 'abcef' from dual
        )
        Select   coln, 
regexp_replace(regexp_substr(coln, '([A-Z]+[[:space:]]*)+'), '[[:space:]][A-Z]$') as B,
regexp_substr(coln, '([[:upper:]][[:lower:]]+[[:space:]]*|[[:lower:]]+)+') as C
From t;