如何在不使用regexp_like的情况下从列中获取整数



我在Oracle中有一个列X,它的值类似于";a1b2c3"abc"1ab"123〃"156-346";

如何编写一个sql查询,只返回包含纯数值(如123,456等(的X

我不能使用regexp_like,因为我正在使用10g

这里有两个选项:一个使用正则表达式,另一个使用translate函数。
SQL> with test as
2    (select 'a1b2c3' col from dual union all
3     select 'abc'        from dual union all
4     select '1ab'        from dual union all
5     select '123'        from dual union all
6     select '156-346'    from dual
7    )
8  select col,
9    regexp_replace(col, '[^[:digit:]]') result,
10    --
11    translate(col, '0'|| translate(col, '$0123456789', '$'), '0') result2
12  from test;
COL     RESULT  RESULT2
------- ------- -------
a1b2c3  123     123
abc
1ab     1       1
123     123     123
156-346 156346  156346
SQL>

使用translate函数删除所有数字并修剪结果时,纯数字将产生null

select x
from test
where trim(translate(x, '0123456789', '          ')) is null;

确保有10个空格作为translate的最后一个参数。

如果x列可以包含空格,则使用

select x
from test
where trim(translate(replace(x,' ','*'), '0123456789', '          ')) is null;

请参阅:http://sqlfiddle.com/#!4/772dc7d/2/0

相关内容

最新更新