열에서 대문자 단어를 찾는 SQL
테이블에 설명 열이 있으며 값은 다음과 같습니다.
This is a EXAMPLE
This is a TEST
This is a VALUE
나는 설명 열에 EXPRESS, TEST, VALUE만 표시하고 싶습니다.
어떻게 이를 달성할 수 있습니까?
방법이 있을 수 있습니다.
-- 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
이 아이디어는 모든 문자열을 토큰화한 다음 대문자로 작성되지 않은 단어를 잘라낸 다음 나머지 단어를 연결하는 것입니다.
결과:
1 EXAMPLE
2 TEST
3 VALUE
4 IS EXAMPLE
다른 문자를 대문자로 처리해야 할 경우,where
일치하는 단어를 필터링할 조건(예: '_' 사용):
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
제공:
1 EXAMPLE
2 TEST
3 VALUE
4 IS EXAMPLE
5 IS AN_EXAMPLE
여기 또 다른 해결책이 있습니다.그것은 알렉세이의 대답에 영감을 받았습니다.
아이디어?모든 단어를 이해하세요.그런 다음 완전히 대문자로만 목록을 집계합니다.
표본 데이터:
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
;
쿼리:
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
결과:
ID | DESCRIPTION | UPPERCASEWORDS
-- | ----------------------------------------- | ------------------
1 | foo Foo FOO bar Bar BAR | FOO,BAR
2 | This is an EXAMPLE TEST Description VALUE | EXAMPLE,TEST,VALUE
REGEXP_REPLACE 기능 덕분에 이를 달성할 수 있습니다.
SELECT REGEXP_REPLACE(my_column, '(^[A-Z]| |[a-z][A-Z]*|[A-Z]*[a-z])', '') AS Result FROM my_table
행의 첫 번째 대문자 문자를 대체하고 모든 소문자 문자와 공백을 공백으로 변환하는 정규식을 사용합니다.
사용해 보십시오.
SELECT SUBSTR(column_name, INSTR(column_name,' ',-1) + 1)
FROM your_table;
이렇게 하면 효과가 있습니다.
SELECT SUBSTR(REGEXP_REPLACE(' ' || REGEXP_REPLACE(description, '(^[A-Z]|[a-z]|[A-Z][a-z]+|[,])', ''), ' +', ' '), 2, 9999) AS only_upper
FROM (
select 'Hey IF you do not know IT, This IS a test of UPPERCASE and IT, with good WILL and faith, Should BE fine to be SHOWN' description
from dual
)
쉼표를 벗기는 조건을 추가했습니다. 제거할 다른 특수 문자를 브래킷 안에 추가할 수 있습니다.
ONLY_UPPER
-----------------------------------
IF IT IS UPPERCASE IT WILL BE SHOWN
이것은 정규식 답변 중 일부를 기반으로 하는 함수입니다.
create or replace function capwords(orig_string varchar2)
return varchar2
as
out_string varchar2(80);
begin
out_string := REGEXP_REPLACE(orig_string, '([a-z][A-Z_]*|[A-Z_]*[a-z])', '');
out_string := REGEXP_REPLACE(trim(out_string), '( *)', ' ');
return out_string;
end;
/
양쪽 끝에 소문자가 있는 대문자 및 밑줄 문자열을 제거합니다.인접한 여러 공간을 하나의 공간으로 바꿉니다.끝에서 추가 공간을 잘라냅니다.최대 크기는 80자로 가정합니다.
약간 편집된 출력:
>select id,str,capwords(str) from test;
ID STR CAPWORDS(STR)
---------- ------------------------------ ------------------
1 This is a EXAMPLE EXAMPLE
2 This is a TEST TEST
3 This is a VALUE VALUE
4 This IS aN EXAMPLE IS EXAMPLE
5 This is WITH_UNDERSCORE WITH_UNDERSCORE
6 ThiS IS aN EXAMPLE IS EXAMPLE
7 thiS IS aN EXAMPLE IS EXAMPLE
8 This IS wiTH_UNDERSCORE IS
열의 값을 변경하지 않고 결과만 "표시"하면 다음을 사용할 수 있습니다.CASE WHEN
(예시)Description
열 이름):
Select CASE WHEN Description like '%EXAMPLE%' then 'EXAMPLE' WHEN Description like '%TEST%' then 'TEST' WHEN Description like '%VALUE%' then 'VALUE' END From [yourTable]
모두 대문자로 써도 조건은 대소문자를 구분하지 않습니다.추가할 수 있습니다.Else '<Value if all conditions are wrong>'
이전에END
값을 포함하지 않는 설명이 있는 경우.예제에서는 이러한 경우에 대해 NULL을 반환하고 쓰기 작업을 수행합니다.ELSE Description
해당 행의 원래 값을 반환합니다.
업데이트가 필요한 경우에도 작동합니다.간단하고 실용적이고, 쉽게 빠져나갈 수 있는 방법이야, 하하.
언급URL : https://stackoverflow.com/questions/51627330/sql-to-find-upper-case-words-from-a-column
'programing' 카테고리의 다른 글
오라클 인덱스 열 값이 null인 경우는 언제입니까? (0) | 2023.07.21 |
---|---|
mvn spring-boot에서 시작한 프로그램에 JVM 옵션을 추가하는 방법:run (0) | 2023.07.21 |
SpringBoot WebClient를 사용할 때 요청을 가로채기 (0) | 2023.07.21 |
구성에서 유형을 정의하는 것을 고려합니다. (0) | 2023.07.21 |
Spring Rest 컨트롤러 상속 (0) | 2023.07.21 |