简体   繁体   English

SQL从列中查找大写单词

[英]SQL to find upper case words from a column

I have a description column in my table and its values are: 我的表中有一个description列,其值为:

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

I want to display only EXAMPLE, TEST, and VALUE from the description column. 我只想从描述列中显示EXAMPLE,TEST和VALUE。

How do I achieve this? 我该如何实现?

This could be a way: 这可能是一种方法:

-- 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

The idea is to tokenize every string, then cut off the words that are not made by upper case characters and then concatenate the remaining words. 想法是标记每个字符串,然后切掉不是由大写字母组成的单词,然后将其余单词连接起来。

The result: 结果:

1    EXAMPLE
2    TEST
3    VALUE
4    IS EXAMPLE

If you need to handle some other character as an upper case letter, you may edit the where condition to filter for the matching words; 如果您需要将其他字符作为大写字母处理,则可以编辑where条件以过滤匹配的单词; for example, with '_': 例如,使用“ _”:

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

gives: 给出:

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

It is possible to achieve this thanks to the REGEXP_REPLACE function: 可以通过REGEXP_REPLACE函数来实现:

SELECT REGEXP_REPLACE(my_column, '(^[A-Z]| |[a-z][A-Z]*|[A-Z]*[a-z])', '') AS Result FROM my_table

It uses a regex which replaces first upper case char of the line and converts every lower case char and space with blanks. 它使用正则表达式替换行的第一个大写字符,并用空格转换每个小写字符和空格。

Here's another solution. 这是另一个解决方案。 It was inspired by Aleksej's answer. 它的灵感来自Aleksej的回答。

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

Try this: 尝试这个:

SELECT SUBSTR(column_name, INSTR(column_name,' ',-1) + 1)
FROM your_table;

This should do the trick: 这应该可以解决问题:

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 
)

I have added condition to strip commas, you can add inside that brakets other special characters to remove. 我已经添加了去除逗号的条件,您可以在内部添加其他要删除的特殊字符。

ONLY_UPPER
-----------------------------------
IF IT IS UPPERCASE IT WILL BE SHOWN

This is a function based on some of the regular expression answers. 这是一个基于某些正则表达式答案的函数。

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;
/

Removes strings of upper case letters and underscores that have lower case letters on either end. 删除两端均为小写字母的大写字母和下划线字符串。 Replaces multiple adjacent spaces with one space. 用一个空格替换多个相邻空格。 Trims extra spaces off of the ends. 修剪两端多余的空间。 Assumes max size of 80 characters. 假定最大大小为80个字符。

Slightly edited output: 稍微编辑的输出:

>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

If you only need to "display" the result without changing the values in the column then you can use CASE WHEN (in the example Description is the column name): 如果只需要“显示”结果而不更改列中的值,则可以使用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]

The conditions are not case sensitive even if you write it all in uppercase. 即使全部用大写字母写,条件也不区分大小写。 You can add Else '<Value if all conditions are wrong>' before the END in case there are descriptions that don't contain any of the values. 您可以在END之前添加Else '<Value if all conditions are wrong>' ,以防某些描述不包含任何值。 The example will return NULL for those cases, and writing ELSE Description will return the original value of that row. 对于这些情况,该示例将返回NULL,并且编写ELSE Description将返回该行的原始值。

It also works if you need to update. 如果您需要更新,它也可以使用。 It is simple and practical, easy way out, haha. 很简单实用,出路容易,哈哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM