SQL to find upper case words from a column

后端 未结 7 1460
南笙
南笙 2020-12-10 02:56

I have a description column in my table and its values are:

This is a EXAMPLE
This is a TEST
This is a VALUE

I want to display only EXAMPLE

7条回答
  •  春和景丽
    2020-12-10 03:30

    Here's another solution. It was inspired by Aleksej's answer.

    The idea? Get all the words. Then aggregate only fully uppercased to a list.

    Sample data:

     create table descriptions (ID int, Description varchar2(100));
    
     insert into descriptions (ID, Description) 
     select 1 as ID, 'foo Foo FOO bar Bar BAR' as Description from dual 
     union all select 2, 'This is an EXAMPLE TEST Description VALUE' from dual
     ;
    

    Query:

     select id, Description, listagg(word, ',') within group (order by pos) as UpperCaseWords
     from (
         select 
          id, Description,
          trim(regexp_substr(Description, '\w+', 1, level)) as word,
          level as pos           
         from descriptions t
         connect by regexp_instr(Description, '\s+', 1, level - 1) > 0
           and prior id = id
           and prior sys_guid() is not null
         )
     where word = upper(word)
     group by id, Description
    

    Result:

    ID | DESCRIPTION                               | UPPERCASEWORDS    
    -- | ----------------------------------------- | ------------------
     1 | foo Foo FOO bar Bar BAR                   | FOO,BAR           
     2 | This is an EXAMPLE TEST Description VALUE | EXAMPLE,TEST,VALUE
    

提交回复
热议问题