oracle中如何判断一个字符串是否数字

整数:
select 1 from dual where ltrim(&a,'0123456789') is null;

浮点数:
select 1 from dual where ltrim(replace(&a,'.'),'0123456789') is null and length(&a)-length(replace(&a,'.'))=1;
负整数:
select 1 from dual where ltrim(replace(&a,'-'),'0123456789') is null and length(&a)-length(replace(&a,'-'))=1 and substr(&a,1,1)='-' ;
select 1 from dual where rtrim(&a),'0123456789')='-';
负浮点数:
select 1 from dual where ltrim(replace(replace(&a,'.'),'-'),'0123456789') is null and length(&a)-length(replace(&a,'.'))=1 and length(&a)-length(replace(&a,'-'))=1 and substr(&a,1,1)='-';
select 1 from dual where rtrim(replace(&a,'.')),'0123456789')='-' and length(&a)-length(replace(&a,'.'))=1 and substr(&a,1,1)='-';--substr(&a,1,1)='-',防止小数点"."出现在第一位

或者,创建函数进行判断
hyacinth在oldwain的基础上进行的修改

代码:


scott
@ORCL> create or replace function isnumber( c varchar2)
2 return number
3 is
4 n number
;
5 begin
6 begin
7 n
:= to_number(c);
~~~~~~~~~~~~~~~~~~
改成n := to_number(nvl(c,'a')) ,否则空值会认为是数字
8 exception when others then
9
return 0;
10 end;
11 return 1;
12 end;
13 /函数已创建。


scott
@ORCL> select isnumber('234') from dual;
ISNUMBER('234')
---------------
1

scott
@ORCL> select isnumber('234b') from dual;
ISNUMBER('234B')
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值