【赛迪网-it技术报道】问题:判断一个字符串的内容是否是数值。
解决方法:利用oracle数据库自带的to_number函数(增加了异常处理部分,防止非数字类型导致函数异常而中断执行)。
sql> create or replace function f_is_num(p_num in varchar2) return varchar2 as
2 v_tmp number;
3 begin
4 if p_num is null then
5 return null;
6 end if;
7 v_tmp := to_number(p_num);
8 return 't';
9 exception
10 when others then
11 return 'n';
12 end;
13 /
函数已创建。
sql> create or replace function f_is_num1(p_num in varchar2) return varchar2 as
2 v_num_dot number default 0;
3 begin
4 if p_num is null then
5 return null;
6 end if;
7 for i in 1..length(p_num) loop
8 case substr(p_num, i, 1)
9 when '0' then null;
10 when '1' then null;
11 when '2' then null;
12 when '3' then null;
13 when '4' then null;
14 when '5' then null;
15 when '6' then null;
16 when '7' then null;
17 when '8' then null;
18 when '9' then null;
19 when '.' then
20 v_num_dot := v_num_dot + 1;
21 if v_num_dot > 1 then
22 return 'n';
23 end if;
24 when '-' then
25 if i != 1 then
26 return 'n';
27 end if;
28 when '+' then
29 if i != 1 then
30 return 'n';
31 end if;
32 else return 'n';
33 end case;
34 end loop;
35 return 't';
36 end;
37 /
函数已创建。
sql> create table t (num_str varchar2(100));
表已创建。
sql> insert into t values ('-5');
已创建 1 行。
sql> insert into t values ('2.2342');
已创建 1 行。
sql> insert into t values ('+123.1234');
已创建 1 行。
sql> insert into t values ('-5-34');
已创建 1 行。
sql> insert into t values ('1230234j342');
已创建 1 行。
sql> insert into t values ('5.524.2');
已创建 1 行。
sql> commit;
提交完成。
sql>
sql> col num_str format a12
sql> col is_num format a6
sql> select num_str, f_is_num(num_str) is_num from t;
num_str is_num
------------ ------
-5 t
2.2342 t
+123.1234 t
-5-34 n
1230234j342 n
5.524.2 n
已选择6行。
sql> select num_str, f_is_num1(num_str) is_num from t;
num_str is_num
------------ ------
-5 t
2.2342 t
+123.1234 t
-5-34 n
1230234j342 n
5.524.2 n
已选择6行。
采用第二种方法在处理较大数据量且其中大部分为非数字类型的数据时,效率较高。
sql> set autot trace stat
sql> set timing on
sql> select f_is_num(object_id) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.04
statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
4809 consistent gets
0 physical reads
0 redo size
82590 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
sql> select f_is_num(object_id) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.04
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4807 consistent gets
0 physical reads
0 redo size
82590 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
sql> select f_is_num1(object_id) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.04
statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
4809 consistent gets
0 physical reads
0 redo size
82591 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
sql> select f_is_num1(object_id) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.04
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4807 consistent gets
0 physical reads
0 redo size
82591 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
假如输入的参数以数字类型为主,则两者效率差不多。
假如输入的参数大部分无法转化为数字类型,则第二种方法的效率会更高。
sql> select f_is_num(object_name) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.08
statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
4809 consistent gets
0 physical reads
0 redo size
82591 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
sql> select f_is_num(object_name) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.07
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4807 consistent gets
0 physical reads
0 redo size
82591 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
sql> select f_is_num1(object_name) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.04
statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
4809 consistent gets
0 physical reads
0 redo size
82592 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
sql> select f_is_num1(object_name) from dba_objects;
已选择6291行。
已用时间: 00: 00: 00.04
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4807 consistent gets
0 physical reads
0 redo size
82592 bytes sent via sql*net to client
5112 bytes received via sql*net from client
421 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6291 rows processed
假如不采用to_number而是使用对字符串中每个字符依次判断的方法,则会增加复杂的程度。
闽公网安备 35060202000074号