简体   繁体   English

选择仅字母,长度为三个字符的字符串

[英]Select alphabetic only strings of three characters length

I have a field in my table that is a varchar and contains alphabetic, numeric, and mixed alphanumeric values. 我的表中有一个varchar字段,包含字母,数字和混合字母数字值。

I would like to select only the values from that field which are 3 characters in length and alphabetic only. 我想只选择该字段中长度为3个字符且仅按字母顺序排列的值。

I am using oracle. 我正在使用oracle。

Sample Data: 样本数据:

AAA
BBB
12345
CCC25
DDDDD

The select statement should only return: select语句应该只返回:

AAA
BBB

I tried the following and it didn't work. 我尝试了以下内容,但没有奏效。 This did not return anything. 这没有任何回报。

select name from MyTable where name like '[a-Z][a-Z][a-Z]';

Then I tried the following thinking it would return all 3-characters long results and it just returned everything: 然后,我尝试了以下方法,以为它会返回所有3个字符的长结果,而它只会返回所有内容:

select name from MyTable where name like '%%%';

You can use the regexp_like function 您可以使用regexp_like函数

SQL> with x as (
  2    select 'aaa' str from dual union all
  3    select 'bbb' from dual union all
  4    select '12345' from dual union all
  5    select 'CCC25' from dual union all
  6    select 'DDDDD' from dual
  7  )
  8  select *
  9    from x
 10   where regexp_like( str, '^[[:alpha:]]{3}$' );

STR
-----
aaa
bbb

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

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