来自char sql的to_number

emm*_*aya 1 sql oracle

我只需要选择只有偶数位数的ID(ID如下:p19,p20等).也就是说,p20是好的(2和0都是偶数); p18不是.

我想用substr从ID中获取每个数字,然后看看它是否均匀.

select from profs 
where to_number(substr(id_prof,2,2))%2=0 and to_number(substr(id_prof,3,2))%2=0;
Run Code Online (Sandbox Code Playgroud)

小智 5

如果您需要所有行在开头包含'p',在偶数上包含偶数数字它应该如下所示:

select *
  from profs 
 where regexp_like (id_prof, '^p[24680]+$');
Run Code Online (Sandbox Code Playgroud)

  • @emma_soraya - 这是否意味着所有数字(不考虑p)都是两位数长?如果没有,你的解决方案将错过p8和p200,因为它在p之后需要两个偶数位.迈克尔的解决方案更为一般. (2认同)