求用一条SQL语句判断一个字符串是否是数字的办法

sailer_shi 2009-01-05 12:19:09
求用一条SQL语句判断一个字符串是否是数字的办法
现在大家都是用函数的方式来判断一个字符串是否是数字,但是现在有没有只用一条SQL语句就能判断字符串是否是数字呢?有的话请各位大侠不吝赐教,谢谢
...全文
1941 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yf520gn 2009-01-05
  • 打赏
  • 举报
回复
用ASSIC和BETWEEN还有 CASE
xugh 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zounf 的回复:]
var info varchar2(100);
declare
dummy_ number;
begin
select to_number('&string') into dummy_ from dual;
:info := 'Is a number string!';
dbms_output.put_line('Is a number string!');

EXCEPTION
WHEN others THEN
:info := 'Is not a number string!';
dbms_output.put_line('Is not a number string!');
end;
/
[/Quote]
以前写过一个,和这个差不多;那时oracle版本比较老
如果是10g,还是用正则表达式吧
多壮志 2009-01-05
  • 打赏
  • 举报
回复
就按照5楼的做了。 如果你的不幸是10g以下。那就不要判断了,出了异常再处理,更节省时间。
也许oracle以后会写个函数is_number ,或者自己写个也行啊。
jinjazz 2009-01-05
  • 打赏
  • 举报
回复
select * from(
select '1' as str from dual union
select '1.1' from dual union
select '1.a' from dual union
select '-6.0' from dual union
select '+6.0'from dual union
select '.9'from dual) a
where NVL(TRANSLATE (str, '.0123456789', '.'),'.') in('+.','-.','.')
/*
STR
+6.0
-6.0
.9
1
1.1*/
ZiSheng 2009-01-05
  • 打赏
  • 举报
回复
select to_number('222') from dual;能够转换,但是不能判断
又是违规昵称 2009-01-05
  • 打赏
  • 举报
回复
留几个测试的例子:

SQL> select 1+1 from dual where REGEXP_LIKE(123, '[[:digit:]]') ;

1+1
----------
2

SQL> select 1+1 from dual where REGEXP_LIKE('aaa', '[[:digit:]]') ;

no rows selected

SQL> select 1+1 from dual where REGEXP_LIKE('222', '[[:digit:]]') ;

1+1
----------
2
又是违规昵称 2009-01-05
  • 打赏
  • 举报
回复
10g可以用正则表达式的方法
select REGEXP_LIKE(字段, [[:digit:]] ) from dual;
flg_inwind 2009-01-05
  • 打赏
  • 举报
回复
mark
方法多种多样啊
清风_X 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lpc19598188 的回复:]
留几个测试的例子:

SQL code
SQL> select 1+1 from dual where REGEXP_LIKE(123, '[[:digit:]]') ;

1+1
----------
2

SQL> select 1+1 from dual where REGEXP_LIKE('aaa', '[[:digit:]]') ;

no rows selected

SQL> select 1+1 from dual where REGEXP_LIKE('222', '[[:digit:]]') ;

1+1
----------
2
[/Quote]

推荐用正则,因为这个方法是通用的
chentony 2009-01-05
  • 打赏
  • 举报
回复
我觉得用to_number 是一种方式

正则表达式还没有用过
dawugui 2009-01-05
  • 打赏
  • 举报
回复
sql server用ISNUMERIC,oracle没找到.帮顶.
yjdfff 2009-01-05
  • 打赏
  • 举报
回复
--测试数据
ID RMAK
------------------
21 litt1232dd
9 litt1232
23 tom
21 125delitt1232
9 ggg
10 123

--SQL语句
SELECT * from a where translate(lower(rmak),'0123456789','0000000000')=rpad('0',length(rmak),'0');

--执行结果只显示是数字的行
ID RMAK
-----------------
10 123
fumuju 2009-01-05
  • 打赏
  • 举报
回复
you can try this

select 1 from dual where ltrim(&a,'0123456789') is null;
zounf 2009-01-05
  • 打赏
  • 举报
回复
var info varchar2(100);
declare
dummy_ number;
begin
select to_number('&string') into dummy_ from dual;
:info := 'Is a number string!';
dbms_output.put_line('Is a number string!');

EXCEPTION
WHEN others THEN
:info := 'Is not a number string!';
dbms_output.put_line('Is not a number string!');
end;
/

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧