现在要加一个条件to_number(某表的attribute1列)=某表的ID列问题是某表的attribute1列有一些不全是纯数字的于是就报错,而纯数字的数据才是我要的数据
怎么办.........
改成某表的attribute1=to_char(某表的ID列)?不行啊.....
某表的ID列是索引列,破坏了索引就慢到根本出不来结果我想问有没有一个强制转字符串到数字的函数呢....
或者怎么去先判断一个字符串能否转数字,然后decode
怎么办.........
改成某表的attribute1=to_char(某表的ID列)?不行啊.....
某表的ID列是索引列,破坏了索引就慢到根本出不来结果我想问有没有一个强制转字符串到数字的函数呢....
或者怎么去先判断一个字符串能否转数字,然后decode
create or replace function is_numeric(p_char in varchar2)
return number
as
v_number number;
begin
v_number:=to_number(p_char);
return 1; --正确
exception
when others then
return 0;
end;
select ...
form...
where....
and
to_number(decode(is_numeric(某表的attribute1列),1,to_number(某表的attribute1列),0 )=某表的ID列1楼可以说说怎么逐个拿出字符串的每个再怎么看它ASC值就好了
历遍整个字符串也不是不可以,只是需要判断的情况太多,代码会写得很长,而且效率肯定是比不上用oracle的内置函数的。
return 0;
end if;
set v_counter = v_counter + 1;end while; -- while v_counter <= length(p_string) do这是DB2的写法,ORACLE也差不多的,你可以参考下。上不上是个态度问题,上不上的了是个能力问题
create index idx_xxx on t(to_char(某表的ID列));
这个方法不错,写一个function,不过不知道用exption来进行处理是不是一个好方法哟,这里推荐另一个做法,就是用translate来做如下
SQL> with temp as
2 (select 'qweqw1231' num from dual
3 union
4 select '123' num from dual)
5 select num from temp where length(translate(upper(num), '0123456789ABCDEFGH
IJKLMNOPQRSTUVWXYZ','0123456789'))=length(num);NUM
---------
123
......
attribute1=upper(attribute1)
and attribute1=lower(attribute1)
条件进行过滤
to_number(decode(is_numeric(某表的attribute1列),1,to_number(某表的attribute1列),0 )=某表的ID列
-->
to_number(decode(translate(upper(某表的attribute1列),'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ., !@#$%^&*()''"<>?','0123456789'),某表的attribute1列,某表的attribute1列))=某表的ID列