仅当字段在字符串中包含任何小写字母时才获取数据

时间:2018-11-12 06:54:45

标签: sql oracle

我试图获取条件为“仅当字符串在第4位和第5位包含低位字符时才获取​​数据”的数据。 以下是条件:

and ascii(substr(RAP01.CRDVER,4,1)) between 97 and 122
and ascii(substr(RAP01.CRDVER,5,1)) between 97 and 122;

表名RAP01和列名CRDVER。

但是它没有获取所有必需的数据。 这种方法正确吗?

2 个答案:

答案 0 :(得分:2)

这个怎么样? 是第8行-查询将返回其第4和第5个字符(substr(col, 4, 2))为小写字母([a-z])的字符串(c表示< em> search 区分大小写)。

SQL> with test (col) as
  2    (select '12Bcfx23' from dual union all
  3     select '123456'   from dual union all
  4     select 'ABCDEFGH' from dual
  5    )
  6  select col, substr(col, 4, 2) sub
  7  from test
  8  where regexp_like(substr(col, 4, 2), '[a-z]', 'c');

COL      SU
-------- --
12Bcfx23 cf

SQL>

答案 1 :(得分:1)

  • 小写字母是ASCII的子集吗?
  • 我可以使用ASCII识别小写吗?

我怎么知道一个字符串是否小写?如果UPPER of stringstring不同,则使用小写字母。

Sample

create table t1 ( a varchar(100) )\\

insert into t1 (a) values ( 'A' )\\
insert into t1 (a) values ( 'a' )\\
insert into t1 (a) values ( 'å' )\\
insert into t1 (a) values ( 'æ' )\\

select (case when a = upper( a )                  <-- compare with upper
        then a || ' Is Upper' 
        else a || ' Has lower' end) as r_upper,

       (case when not regexp_like(a, '[a-z]', 'c') <-- using ascii
        then a || ' Is Upper' 
        else a || ' Has lower' end) as r_reg

       from t1\\

R_UPPER     | R_REG 
-------------------------
A Is Upper  | A Is Upper
a Has lower | a Has lower
å Has lower | å Is Upper (¹)
æ Has lower | æ Is Upper (¹)

(¹)使用ASCII错误的结果。