sql >> Database teknologi >  >> RDS >> Oracle

SQL til at finde ord med store bogstaver fra en kolonne

Dette kunne være en måde:

-- a test case with test(id, str) as ( select 1, 'This is a EXAMPLE' from dual union all select 2, 'This is a TEST' from dual union all select 3, 'This is a VALUE' from dual union all select 4, 'This IS aN EXAMPLE' from dual ) -- concatenate the resulting words select id, listagg(str, ' ') within group (order by pos) from ( -- tokenize the strings by using the space as a word separator SELECT id, trim(regexp_substr(str, '[^ ]+', 1, level)) str, level as pos FROM test t CONNECT BY instr(str, ' ', 1, level - 1) > 0 and prior id = id and prior sys_guid() is not null ) -- only get the uppercase words where regexp_like(str, '^[A-Z]+$') group by id

Ideen er at tokenisere hver streng, derefter afskære de ord, der ikke er lavet af store bogstaver og derefter sammenkæde de resterende ord.

Resultatet:

1    EXAMPLE
2    TEST
3    VALUE
4    IS EXAMPLE
 

Hvis du har brug for at håndtere et andet tegn som et stort bogstav, kan du redigere where betingelse for at filtrere efter de matchende ord; for eksempel med '_':

with test(id, str) as (
select 1, 'This is a EXAMPLE' from dual union all
select 2, 'This is a TEST' from dual union all
select 3, 'This is a VALUE' from dual union all
select 4, 'This IS aN EXAMPLE' from dual union all
select 5, 'This IS AN_EXAMPLE' from dual
)
select id, listagg(str, ' ') within group (order by pos)
from (
    SELECT id,
           trim(regexp_substr(str, '[^ ]+', 1, level)) str,
           level as pos           
      FROM test t
    CONNECT BY instr(str, ' ', 1, level - 1) > 0
      and prior id = id
      and prior sys_guid() is not null
    )
where regexp_like(str, '^[A-Z_]+$')   
group by id
 

giver:

1   EXAMPLE
2   TEST
3   VALUE
4   IS EXAMPLE
5   IS AN_EXAMPLE
 


  1. Sådan installeres og konfigureres phpMyAdmin på Debian 8

  2. Kan jeg parametrisere tabelnavnet i en forberedt erklæring?

  3. Hvordan man bedst deler csv-strenge i oracle 9i

  4. Hvordan kan jeg indstille en SQL Server-forbindelsesstreng?