在oracle中,select語句查詢字段中非純數字值


最近,將原來的數字符字段轉換為數字時,總報錯誤:無效數字。

如何找出其中哪些是非數字字符的記錄?比較麻煩的事。下面是用Oracle DB自帶的函數translate可以找出來的

1.創建測試表

Create Table TestChar(
    ITEM_NUMBER VARCHAR2(20)
);

 

2.手工插入測試記錄

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;

 

3.妙用Oracle 內置函數Translate找出非數字字符的記錄

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

 

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

=======================================================

再看看另外一個寫的,也比較實用。

--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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM