如何判断一个值 是否为正整数

feixianxxx 2011-03-03 10:18:36


请问在plsql中 如何判断一个值 是否为正整数
要求包含特殊字符 字母 文字 不能为负数 0 也不能为小数

急~~
...全文
941 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingjielva 2012-10-15
  • 打赏
  • 举报
回复
floor(2.01)<>2.01
feixianxxx 2011-03-08
  • 打赏
  • 举报
回复
这个是itpub的

多谢大家的答案~
心中的彩虹 2011-03-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 feixianxxx 的回复:]
请问在plsql中 如何判断一个值 是否为正整数
要求包含特殊字符 字母 文字 不能为负数 0 也不能为小数

急~~
[/Quote]

---根据mssql isnumeric改进判断是否为正整数 1为正整数,0为数字,字符等
create or replace function f_isnumeric(cnt varchar2) return number
as
res number;
flag number;
begin
if cnt is null then
return 0;
end if;
select to_number(cnt) into res from dual;
select decode(instr(res,'.')+sign(res),1,1,0) into flag from dual;
if flag=1 then
return 1;
else
return 0;
end if;
exception
when others then
return 0;
end;


SQL> create or replace function f_isnumeric(cnt varchar2) return number
2 as
3 res number;
4 flag number;
5 begin
6 if cnt is null then
7 return 0;
8 end if;
9 select to_number(cnt) into res from dual;
10 select decode(instr(res,'.')+sign(res),1,1,0) into flag from dual;
11 if flag=1 then
12 return 1;
13 else
14 return 0;
15 end if;
16 exception
17 when others then
18 return 0;
19 end;
20 /

Function created

SQL> select f_isnumeric('s'),f_isnumeric(10),f_isnumeric(1.2) from dual
/
2 /

F_ISNUMERIC('S') F_ISNUMERIC(10) F_ISNUMERIC(1.2)
---------------- --------------- ----------------
0 1 0

SQL> select f_isnumeric('s'),f_isnumeric(10),f_isnumeric(1.2) from dual

2 /

F_ISNUMERIC('S') F_ISNUMERIC(10) F_ISNUMERIC(1.2)
---------------- --------------- ----------------
0 1 0

where F_ISNUMERIC(col)=1
yukiMark 2011-03-04
  • 打赏
  • 举报
回复
decode真是个好东西。。。
心中的彩虹 2011-03-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 feixianxxx 的回复:]
请问在plsql中 如何判断一个值 是否为正整数
要求包含特殊字符 字母 文字 不能为负数 0 也不能为小数

急~~
[/Quote]

--也可以这样

with tb as
(select '1' res from dual union all
select '13' res from dual union all
select 's2' from dual union all
select '1.2' from dual union all
select '稳靠0.2' from dual union all
select '0.2' from dual union all
select '-10' from dual)
select res from tb where length(res)=length(regexp_replace(res,'[[:alpha:]]',''))
and decode(instr(res,'.')+sign(regexp_replace(res,'[[:alpha:]]','')),1,1,0)=1



SQL>
SQL> with tb as
2 (select '1' res from dual union all
3 select '13' res from dual union all
4 select 's2' from dual union all
5 select '1.2' from dual union all
6 select '稳靠0.2' from dual union all
7 select '0.2' from dual union all
8 select '-10' from dual)
9 select res from tb where length(res)=length(regexp_replace(res,'[[:alpha:]]',''))
10 and decode(instr(res,'.')+sign(regexp_replace(res,'[[:alpha:]]','')),1,1,0)=1
11 /

RES
-------
1
13

SQL>




lxyzxq2008 2011-03-03
  • 打赏
  • 举报
回复
首先判断是不是数字,然后判断是否包含小数点,最后判断>0就可以了吧?

或者用正则表达式
iqlife 2011-03-03
  • 打赏
  • 举报
回复
用正则表达式,匹配到 0-9以外的都不符合你的条件,
feixianxxx 2011-03-03
  • 打赏
  • 举报
回复
多谢各位 挺急的

17,086

社区成员

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

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