Oracle 判斷是否為數字型(金額型)

來源:互聯網
上載者:User

在sql server,校正資料是否為數字型比較容易,有提供的方法可以使用:

ISNUMERIC

當是數字型,方法傳回值為1;否則傳回值為0

例:

select * from tablename where isnumeric(data) = 1

在Oracle中沒有這樣的方法,要實現判斷是否為數字型需要自己寫一個方法來實現。但也可以用其它方法:

方法一,用正則的方法:

使用 regexp_like

例:

select * from tablename where

regexp_like(trim(data),'^([\-]?[0-9]+\.[0-9]+)$|^([\-]?[0-9])+$|^([\-]?[0-9]{1}\.[0-9]+E[\-]?[0-9])+$')

這個正則分三部分:

1、^([\-]?[0-9]+\.[0-9]+)$

浮點型,包括帶小數點的情況

2、^([\-]?[0-9])+$

整數型

3、^([\-]?[0-9]{1}\.[0-9]+E[\-]?[0-9])+$

科學計數法表示的數字

以上三部分均包括了負數的情況

註:regexp_like適用於oracle 10g及其之後的版本,之前的不適用。

方法二,用正則的方法:

使用translate方法

例:

select data,decode(trim(translate(trim(cdata),'0123456789.',' ')),'','is number','not number')

from tablename

但上面這個還不太完善,沒有考慮負數,科學計數法表示,資料本身為空白,或對“.”的驗證。

所以使用方法二的時候要注意,或修改之(寫得比較完善有點麻煩,不如寫個方法了)。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.