Oracle語句判斷字符串是否爲數字及translate函數解析

判斷字符串是否全爲數字一、使用 trim+translate 函數二、使用 replace+translate 函數三、使用 regexp_like 函數translate 函數講解一、基本用法,字符對應替換二、 若是 to_string 沒有對應字符則刪除額外的字符三、若是 to_string 對應字符過多不影響四、若是 to_string 爲空字符,則直接返回 NULL五、若是想刪除 expr 中某些字符,除了 from_strimg 擁有這些字符外,還應傳入一個不相關字符,同時 to_string 中也須要傳入該字符六、 若是 from_strimg 相同字符對應 to_string 多個字符,按去第一個;八、判斷 from_strimg 中包含的字符是否一致(排序能夠不一樣)九、同 replace 函數比較web

判斷字符串是否全爲數字

最近在工做中遇到這樣一個問題,判斷字符串中是否全爲數字,Oracle 數據庫的操做方法有如下幾種:正則表達式

一、使用 trim+translate 函數

select * from dual where trim(translate(column,'0123456789',' ')) is NULL;
複製代碼

這裏要注意的是:translate 函數的第三個參數是一個空格,不是'', 由於translate的第三個參數若是爲空的話,那麼永遠返回'',這樣的就不能達到過濾純數字的目的。這樣把全部的數字都轉化爲空格,若是所有是由數 構成,那麼一旦trim後天然是空,實現了上述目標。固然若是想排除空項的話,能夠這樣寫:sql

select * from dual where trim(translate(nvl(column,'x'),'0123456789',' ')) is NULL;--x 表示任何'0-9'之外的字符。
複製代碼

NVL函數是一個空值轉換函數 數據庫

NVL(exp1,exp2)函數,若是exp1爲空值,則返回exp2;不然返回exp1。app

注意:當 column 的值爲空格時,也會被篩選到,因此比較好的方法是使用 replace+translate 函數。函數

二、使用 replace+translate 函數

select * from dual where replace(translate(column,'0123456789','0'),'0',''is NULL;
複製代碼

三、使用 regexp_like 函數

select * from dual where regexp_like(column,'^[0-9]+[0-9]$')
複製代碼

這裏須要注意的是:regexp_like 函數不是在全部的 Oracle 版本中都能使用。regexp_like 是 Oracle 支持正則表達式的函數中的一個,共有四個函數:regexp_like ,regexp_replace,regexp_instr,regexp_substr。url

translate 函數講解

上述方法比較關鍵的是 translate 函數,對於該函數查看相關講解以下:spa

translate(expr, from_strimg, to_string)
複製代碼

translate 函數返回 expr,其中 from_string 中的每一個字符都被 to_string 中的相應字符替換。若 expr 中某個字符未出如今 from_string 中,則該字符不會被替換。若是 expr 是一個字符串,那麼你必須把它放在單引號中。 from_string 參數值能夠包含比 to_string 更多的字符。在這種狀況下,from_string 末尾的多餘字符在 to_string 中沒有對應的字符。若是這些額外的字符出如今 expr 中,那麼它們將從 expr 返回值中移除。code

to_string 參數值爲空字符串時,expr 返回值中刪除 from_string 中的全部字符。Oracle 數據庫將空字符串解釋爲空,若是此函數具備空參數,則返回 null。regexp

translate 提供了與 replace 函數相關的功能。 replace 容許用一個字符串替換另外一個字符串,以及刪除字符串。 translate 容許在一個操做中進行多個單字符,一對一的替換。

實際案例:

一、基本用法,字符對應替換

select translate('1234567','123' ,'abc'from dual ;--1替換爲a,2替換爲b,3替換爲c
-------
abc4567
複製代碼

二、 若是 to_string 沒有對應字符則刪除額外的字符

select translate('1234567','123' ,'ab'from dual;--‘1234567’中的‘3’被刪掉;
-----
ab4567
複製代碼

三、若是 to_string 對應字符過多不影響

select translate('1234567','123' ,'abccd'from dual;    ---‘123’對應‘abc’
複製代碼

四、若是 to_string 爲空字符,則直接返回 NULL

select translate('1234567','123' ,''from dual;
----
null
複製代碼

五、若是想刪除 expr 中某些字符,除了 from_strimg 擁有這些字符外,還應傳入一個不相關字符,同時 to_string 中也須要傳入該字符

SELECT  translate('0123456789','@123' ,'@'FROM dual
----
0456789
複製代碼

其實原理和 3 同樣,'@123'比'@'長,額外的'123'字符在 expr 中出現,所以去除掉 expr 中的’123‘。

須要注意的是,若是更改'@'字符在 from_strimg 中的位置,結果就不同了。

SELECT  translate('0123456789','123@' ,'@'FROM dual
----
0@456789
複製代碼

緣由在於'123@'中的'1'字符和'@'對應,則'23@'就屬於額外的字符,從 expr 返回值中刪除'23'字符。

六、 若是 from_strimg 相同字符對應 to_string 多個字符,按去第一個;

SELECT  translate('01233456789','1233' ,'abcd'FROM dual
----
0abcc456789
複製代碼

七、 若是想保留某些特定字符篩選掉其餘的

好比篩掉漢字保留數字

先刪除數字

SELECT  translate('我5喜2歡1你','@0123456789' ,'@'FROM dual
-----
我喜歡你
複製代碼

再用篩選出的漢字去篩選原來的語句留下數字,

SELECT  translate('我5喜2歡1你','@'||translate('我5喜2歡1你','@0123456789' ,'@') ,'@'FROM dual
----
521
複製代碼

八、判斷 from_strimg 中包含的字符是否一致(排序能夠不一樣)

SELECT  1 FROM dual where translate('abcdefgh','bcd' ,'111111')=translate('abcdefgh','cbd' ,'111111')
----
1
----反之
SELECT  1 FROM dual where translate('abcdefgh','bdd' ,'111111')=translate('abcdefgh','cbd' ,'111111')
----
null
複製代碼

九、同 replace 函數比較

select translate('itmyhome#163%com''#%''@.'from dual;
select replace('itmyhome#163%com''#%''@.'from dual;

---------
itmyhome@163.com
itmyhome#163%com
複製代碼
相關文章
相關標籤/搜索