许多用过asp的朋友对isnumeric函数都有一定的了解,这是一个常用的判断入参是否为数字的函数。而在oracle中没有现成的判断是否为数字函数,下面我们就来介绍一下如何用三种方法来将其实现:
1. 利用 to_number
create or replace function isnumeric (str in varchar2)
return number
is
v_str float;
begin
if str is null
then
return 0;
else
begin
select to_number (str)
into v_str
from dual;
exception
when invalid_number
then
return 0;
end;
return 1;
end if;
end isnumeric;
|
2. 利用 regexp_like
create or replace function isnumeric (str in varchar2)
return number
is
begin
if str is null
then
return 0;
else
if regexp_like (str, '^(-{0,1}+{0,1})[0-9]+(.{0,1}[0-9]+)$')
then
return 1;
else
return 0;
end if;
end if;
end isnumeric;
|
3. 利用 translate
create or replace function isnumeric (str in varchar2)
return number
is
v_str varchar2 (1000);
begin
if str is null
then
return 0;
else
v_str := translate (str, '.0123456789', '.');
if v_str = '.' or v_str = '+.' or v_str = '-.' or v_str is null
then
return 1;
else
return 0;
end if;
end if;
end isnumeric;
|