共計 1585 個字符,預(yù)計需要花費(fèi) 4 分鐘才能閱讀完成。
自動寫代碼機(jī)器人,免費(fèi)開通
這篇文章給大家介紹 translate 與 replace 函數(shù)怎么在 Oracle 中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
translate 函數(shù)語法:
translate(expr, from_strimg, to_string)
簡介:
translate 返回 expr,其中 from_string 中的每個字符的所有出現(xiàn)都被 to_string 中的相應(yīng)字符替換。expr 中不在 from_string 中的字符不會被替換。如果 expr 是一個字符串,那么你必須把它放在單引號中。from_string 的參數(shù)可以包含比 to_string 更多的字符。在這種情況下,from_string 末尾的多余字符在 to_string 中沒有對應(yīng)的字符。如果這些額外的字符出現(xiàn)在字符中,那么它們將從返回值中移除。
您不能使用 to_string 的空字符串從返回值中刪除 from_string 中的所有字符。Oracle 數(shù)據(jù)庫將空字符串解釋為空,如果此函數(shù)具有空參數(shù),則返回 null。
translate 提供了與 replace 函數(shù)相關(guān)的功能。replace 讓你用一個字符串替換另一個字符串,以及刪除字符串。translate 允許您在一個操作中進(jìn)行多個單字符,一對一的替換。
該函數(shù)不直接支持 CLOB 數(shù)據(jù)。但是,CLOB 可以通過隱式數(shù)據(jù)轉(zhuǎn)換作為參數(shù)傳遞。
例子:
以下語句將一句話轉(zhuǎn)換為具有下劃線分隔的字符串。from_string 包含四個字符:井號,美元符號,空格,星號。to_string 只包含一個 @符號和兩個下劃線。這使得 from_string 中的第四個字符沒有相應(yīng)的替換,所以星號從返回的值中刪除。
SELECT TRANSLATE(itmyhome#163.com$is my* email , #$ * , @__) from dual
----------
itmyhome@163.com_is_my_email
replace 函數(shù)
語法:
REPLACE(char, search_string,replacement_string)
用法:
將 char 中的字符串 search_string 全部轉(zhuǎn)換為字符串 replacement_string。
舉例:
SQL select REPLACE(fgsgswsgs , fk , j) 返回值 from dual;
返回值
---------
fgsgswsgs
SQL select REPLACE(fgsgswsgs , sg , eeerrrttt) 返回值 from dual;
返回值
-----------------------
fgeeerrrtttsweeerrrttts
分析:
第一個例子中由于 fgsgswsgs 中沒有與 fk 匹配的字符串,
故返回值仍然是 fgsgswsgs;
第二個例子中將 fgsgswsgs 中的字符串 sg 全部轉(zhuǎn)換為 eeerrrttt。
總結(jié):綜上所述,replace 與 translate 都是替代函數(shù),
只不過 replace 針對的是字符串,而 translate 針對的是單個字符。
和 replace 函數(shù)的區(qū)別
select translate(itmyhome#163%com , #% , @.) from dual;
select replace(itmyhome#163%com , #% , @.) from dual;
---------
itmyhome@163.com
itmyhome#163%com
上面的 translate 函數(shù)是將 #替換為 @,% 替換為.
而 replace 卻沒有實(shí)現(xiàn)此效果,是因?yàn)闆]有找到 #% 整體的組合
關(guān)于 translate 與 replace 函數(shù)怎么在 Oracle 中使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
向 AI 問一下細(xì)節(jié)