在oracle中,select语句查询字段中非纯数字值

最近,将原来的数字符字段转换为数字时,总报错误:无效数字。html

如何找出其中哪些是非数字字符的记录?比较麻烦的事。下面是用Oracle DB自带的函数translate能够找出来的函数

1.建立测试表测试

Create Table TestChar(
    ITEM_NUMBER VARCHAR2(20)
);spa

 

2.手工插入测试记录.net

Insert Into TestChar (ITEM_NUMBER) values ('312');
Insert Into TestChar (ITEM_NUMBER) values ('312');
Insert Into TestChar (ITEM_NUMBER) values ('4412');
Insert Into TestChar (ITEM_NUMBER) values ('152');
Insert Into TestChar (ITEM_NUMBER) values ('162');
Insert Into TestChar (ITEM_NUMBER) values ('172');
Insert Into TestChar (ITEM_NUMBER) values ('142');
Insert Into TestChar (ITEM_NUMBER) values ('142');
Insert Into TestChar (ITEM_NUMBER) values ('112');
Insert Into TestChar (ITEM_NUMBER) values ('1d2');
Insert Into TestChar (ITEM_NUMBER) values ('152');
Insert Into TestChar (ITEM_NUMBER) values ('125');
Insert Into TestChar (ITEM_NUMBER) values ('162');
Insert Into TestChar (ITEM_NUMBER) values ('712');
Insert Into TestChar (ITEM_NUMBER) values ('A712');
commit;code

 

3.妙用Oracle 内置函数Translate找出非数字字符的记录regexp

select trim(translate(RTRIM(LTRIM(ITEM_NUMBER)), '#0123456789', '#'))
from TestChar
Where trim(translate(RTRIM(LTRIM(ITEM_NUMBER)), '#0123456789', '#')) is not null;htm

 

出处:http://blog.csdn.net/chenxianping/article/details/6338045blog

=======================================================get

再看看另一个写的,也比较实用。

--1.正则判断,适用于10g以上版本
--非正整数 
select 字段 fromwhere regexp_replace(字段,'\d','') is not null;
--非数值类型
select 字段 fromwhere regexp_replace(字段,'^[-\+]?\d+(\.\d+)?$','') is not null;
--2.自定义函数,判断非值类型
create or replace function isnumber(col varchar2) return <a href="https://www.baidu.com/s?wd=integer&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YdmWndnynknWfzrjDYuHF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjDvPjDYPHDs" target="_blank" class="baidu-highlight">integer</a> is
  i number;
begin
  i := to_number(col);
  return 1;
exception
  when others then
    return 0;
end;
select 字段 fromwhere isnumber(字段)=0;

 

出处:https://zhidao.baidu.com/question/416414510.html