to bzszp(SongZip) : 你试过吗? 如果a为数值型,我试过的,用"a = trunc(a)"可以啊 你为什么说不可以呢? set serveroutput on Declare i integer:=20; begin if i=trunc(i) then dbms_output.put_line('is integer'); else dbms_output.put_line('is not integer'); end if; end;SQL> 11 / is integerPL/SQL procedure successfully completed
以下分别是整型与非整型的测试: set serveroutput on Declare i float:=20; j float:=20.5; k integer; begin k:=trunc(i); if i=k then dbms_output.put_line('i is integer'); else dbms_output.put_line('i is not integer'); end if; if j=k then dbms_output.put_line('j is integer'); else dbms_output.put_line('j is not integer'); end if; end;SQL> 18 / i is integer j is not integerPL/SQL procedure successfully completed
SQL> set serveroutput on SQL> declare 2 a number:=10; 3 b varchar2(10):='123fff'; 4 begin 5 begin 6 a:=trunc(a); 7 dbms_output.put_line('a is integer'); 8 exception 9 when others then 10 dbms_output.put_line('a is not integer'); 11 end; 12 begin 13 b:=trunc(b); 14 dbms_output.put_line('b is integer'); 15 exception 16 when others then 17 dbms_output.put_line('b is not integer'); 18 end; 19 end; 20 / a is integer b is not integerPL/SQL procedure successfully completed
SQL> declare 2 a int; 3 b number:=10.5; 4 begin 5 a:=b; 6 dbms_output.put_line('b is integer'); 7 exception 8 when others then 9 dbms_output.put_line('b is not integer'); 10 end; 11 / b is integer 以上的是判断不了 以下可以参考: SQL> declare 2 a number:=10.5; 3 begin 4 if instr(a,'.')>0 then 5 dbms_output.put_line('a is not integer'); 6 else 7 dbms_output.put_line('a is integer'); 8 end if; 9 end; 10 / a is not integerPL/SQL procedure successfully completed
当然试过了 1:如果该字符为数字字符,不能判断出来 SQL> select 1 from dual where '11'=trunc('11'); 1 ---------- 12:如果为非数字的字符串,报错 SQL> select 1 from dual where 'i'=trunc('i'); select 1 from dual where 'i'=trunc('i') * ERROR 位于第 1 行: ORA-01722: 无效数字oracle好像自动进行to_char(),to_number()转换 SQL> select 1 from dual where '11.1'=trunc('11.1');未选定行 SQL> select 1 from dual where 11.1=trunc('11.1');未选定行SQL> select 1 from dual where 11=trunc('11'); 1 ---------- 1SQL> select 1 from dual where '11'=trunc(11); 1 ---------- 1SQL>
如果传字符串的话,会报错!
不能判断出来
你试过吗?
如果a为数值型,我试过的,用"a = trunc(a)"可以啊
你为什么说不可以呢? set serveroutput on
Declare
i integer:=20;
begin
if i=trunc(i) then
dbms_output.put_line('is integer');
else
dbms_output.put_line('is not integer');
end if;
end;SQL>
11 /
is integerPL/SQL procedure successfully completed
set serveroutput on
Declare
i float:=20;
j float:=20.5;
k integer;
begin
k:=trunc(i);
if i=k then
dbms_output.put_line('i is integer');
else
dbms_output.put_line('i is not integer');
end if;
if j=k then
dbms_output.put_line('j is integer');
else
dbms_output.put_line('j is not integer');
end if;
end;SQL>
18 /
i is integer
j is not integerPL/SQL procedure successfully completed
SQL> declare
2 a number:=10;
3 b varchar2(10):='123fff';
4 begin
5 begin
6 a:=trunc(a);
7 dbms_output.put_line('a is integer');
8 exception
9 when others then
10 dbms_output.put_line('a is not integer');
11 end;
12 begin
13 b:=trunc(b);
14 dbms_output.put_line('b is integer');
15 exception
16 when others then
17 dbms_output.put_line('b is not integer');
18 end;
19 end;
20 /
a is integer
b is not integerPL/SQL procedure successfully completed
2 a int;
3 b number:=10.5;
4 begin
5 a:=b;
6 dbms_output.put_line('b is integer');
7 exception
8 when others then
9 dbms_output.put_line('b is not integer');
10 end;
11 /
b is integer
以上的是判断不了
以下可以参考:
SQL> declare
2 a number:=10.5;
3 begin
4 if instr(a,'.')>0 then
5 dbms_output.put_line('a is not integer');
6 else
7 dbms_output.put_line('a is integer');
8 end if;
9 end;
10 /
a is not integerPL/SQL procedure successfully completed
1:如果该字符为数字字符,不能判断出来
SQL> select 1 from dual where '11'=trunc('11'); 1
----------
12:如果为非数字的字符串,报错
SQL> select 1 from dual where 'i'=trunc('i');
select 1 from dual where 'i'=trunc('i')
*
ERROR 位于第 1 行:
ORA-01722: 无效数字oracle好像自动进行to_char(),to_number()转换
SQL> select 1 from dual where '11.1'=trunc('11.1');未选定行
SQL> select 1 from dual where 11.1=trunc('11.1');未选定行SQL> select 1 from dual where 11=trunc('11'); 1
----------
1SQL> select 1 from dual where '11'=trunc(11); 1
----------
1SQL>
对于数值型字符,好像没办法判断出来
对于非数值型字符,通过trunc(a)抛出异常可判断出来
对于数值型数字,通过a 是否等于trunc(a) 可判断出来