繁体   English   中英

ORACLE-如果Column仅包含String,则为True,否则为False

[英]ORACLE- If Column contains only String then True else False

假设我有2列,第一列包含字母数字以及字符的混合,例如。 abc12 $$,abc,123,re12。 第二列仅包含数字,但长度不同。

我必须查找第一列是否仅包含字母,第二列是否仅包含9位数字,然后为True,否则为False。

你能帮我吗?

以下是您可以修改的查询

with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual
union select 'abc3' from dual
union select 'ab3c' from dual)
select * from temp;

要检查一列是否仅包含字符,可以使用:

case when regexp_like(aColumn, '^[[:alpha:]]*$') then 'true' else 'false' end

这会将空字符串视为字符串; 如果要为空字符串获取false,则可以将[[:alpha:]]*编辑为[[:alpha:]]+

这个怎么运作:

  • ^ :字符串的开头
  • [[:alpha:]]*零个或多个字母字符
  • $ :字符串的结尾

要检查第二列是否仅包含9位数字,您可能需要

case when regexp_like(aColumn, '^\d{9}$') then 'true' else 'false' end

\\d{9}在这里的意思是“恰好出现9个数字”。

您可以轻松地使用这两个表达式一起进行两项检查,例如

case
  when regexp_like(column_1, '^[[:alpha:]]*$')  AND
       regexp_like(column_2, '^\d{9}$')
  then 'true'
  else 'false'
end

这可能会有所帮助

with temp(name,numb) as 
(select 'abc',123456789 from dual
union select '123abc',123 from dual
union select '1abc3',123 from dual
union select 'abc3',123 from dual
union select 'ab3c',123 from dual)
select name,case when regexp_like(name, '^[a-zA-Z]*$') and 
regexp_like(numb, '^[^a-zA-Z]*$') and length(to_char(numb))=9 then 'true' else 'false' end from temp;

我认为您正在尝试提出以下要求:

with temp(str,nr) as
 (select 'abc' str,123456789 nr
    from dual
  union
  select '123abc$',12345678
    from dual
  union
  select '1abc3#' ,1234567
    from dual
  union
  select 'abc3'   ,123456
    from dual
  union
  select 'ab3c'   ,12345 
    from dual)
select case when ( t.str = regexp_substr(str,'[a-zA-Z]+') and length(nr)=9 ) then 'True' 
            else 'False' 
        end result,
       str, nr 
  from temp t
 order by length(nr) desc;

RESULT  STR        NR
------  -------    ---------
True    abc        123456789
False   123abc$    12345678
False   1abc3#     1234567
False   abc3       123456
False   ab3c       12345

SQL小提琴演示

暂无
暂无

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

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