如何在不使用 regexp_like 的情况下从列中获取整数
How to get integer from column without using regexp_like
我在 Oracle
中有一个列 X
,其值类似于“a1b2c3”、“abc”、“1ab”、“123”、“156-346”
如何编写一个 sql 查询,使 returns 只有 X
包含 123,456
等纯数值
我不能使用 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, '23456789', '$'), '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;
我在 Oracle
中有一个列 X
,其值类似于“a1b2c3”、“abc”、“1ab”、“123”、“156-346”
如何编写一个 sql 查询,使 returns 只有 X
包含 123,456
等纯数值
我不能使用 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, '23456789', '$'), '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;