Oracle 函数replace和translate的比较
2008-11-06 16:17
351 查看
今天看了SQLCOOKBOOK中的一个例子,其中看到了两个函数Replace和Translate时产生了疑惑,感觉这两个函数的作用是一样的,书上面的例子也看的不是很明白,Google了一下,看了Oracle的官方解释,终于彻底明白了。官方的解释如下:
REPLACE
Descriptionoftheillustrationreplace.gif
Purpose
Both
SeeAlso:
TRANSLATE
Examples
Thefollowingexamplereplacesoccurrencesof
REPLACE
Purpose
REPLACEreturns
charwitheveryoccurrenceof
search_stringreplacedwith
replacement_string.If
replacement_stringisomittedornull,thenalloccurrencesof
search_stringareremoved.If
search_stringisnull,then
charisreturned.
Both
search_stringand
replacement_string,aswellas
char,canbeanyofthedatatypes
CHAR,
VARCHAR2,
NCHAR,
NVARCHAR2,
CLOB,or
NCLOB.Thestringreturnedisinthesamecharactersetas
char.Thefunctionreturns
VARCHAR2ifthefirstargumentisnotaLOBandreturns
CLOBifthefirstargumentisaLOB.
REPLACEprovidesfunctionalityrelatedtothatprovidedbythe
TRANSLATEfunction.
TRANSLATEprovidessingle-character,one-to-onesubstitution.
REPLACEletsyousubstituteonestringforanotheraswellastoremovecharacterstrings.
SeeAlso:
Examples
Thefollowingexamplereplacesoccurrencesof
Jwith
BL:
SELECTREPLACE('JACKandJUE','J','BL')"Changes" FROMDUAL; Changes -------------- BLACKandBLUE
Translate
Descriptionoftheillustrationtranslate.gif
Purpose
TRANSLATEreturns
exprwithalloccurrencesofeachcharacterin
from_stringreplacedbyitscorrespondingcharacterin
to_string.Charactersin
exprthatarenotin
from_stringarenotreplaced.If
exprisacharacterstring,thenyoumustencloseitinsinglequotationmarks.Theargument
from_stringcancontainmorecharactersthan
to_string.Inthiscase,theextracharactersattheendof
from_stringhavenocorrespondingcharactersin
to_string.Iftheseextracharactersappearin
char,thentheyareremovedfromthereturnvalue.[/code]
Youcannotuseanemptystringfor[code]to_stringtoremoveallcharactersin
from_stringfromthereturnvalue.OracleDatabaseinterpretstheemptystringasnull,andifthisfunctionhasanullargument,thenitreturnsnull.[/code]
TRANSLATEprovidesfunctionalityrelatedtothatprovidedbythe
REPLACEfunction.
REPLACEletsyousubstituteasinglestringforanothersinglestring,aswellasremovecharacterstrings.
TRANSLATEletsyoumakeseveralsingle-character,one-to-onesubstitutionsinoneoperation.[/code]
Thisfunctiondoesnotsupport[code]CLOBdatadirectly.However,
CLOBscanbepassedinasargumentsthroughimplicitdataconversion.[/code]
SeeAlso:"DatatypeComparisonRules"formoreinformationand REPLACE
Examples
Thefollowingstatementtranslatesabooktitleintoastringthatcouldbeused(forexample)asafilename.The[code]from_stringcontainsfourcharacters:aspace,asterisk,slash,andapostrophe(withanextraapostropheastheescapecharacter).The
to_stringcontainsonlythreeunderscores.Thisleavesthefourthcharacterinthe
from_stringwithoutacorrespondingreplacement,soapostrophesaredroppedfromthereturnedvalue.[/code]
SELECTTRANSLATE('SQL*PlusUser''sGuide','*/''','___')FROMDUAL;
TRANSLATE('SQL*PLUSU
--------------------
SQL_Plus_Users_Guide
selecttranslate('liyan4h123ui','#liyanhui','#')fromdual
结果:4123
selecttranslate('asadad434323','#0123456789','#')fromdual
结果:asadad
selectTRANSLATE('kkaxksx','kx','12')fromdual
结果:11a21s2
相关文章推荐
- Oracle 函数replace和translate的比较
- Oracle的Replace函数与translate函数详解与比较
- Oracle translate 函数的用法, 以及和replace的区别
- Oracle translate 函数的用法, 以及和replace的区别
- Oracle translate 函数的用法, 以及和replace的区别
- 【ORACLE replace和translate函数详解】
- ORACLE replace和translate函数详解
- ORACLE replace和translate函数详解
- Oracle的Replace函数与translate函数详解与比较
- Oracle 中 replace函数和translate函数比较
- 【转载】ORACLE replace和translate函数详解
- Oracle的Replace函数与translate函数详解与比较
- 【转】Oracle的Replace函数与translate函数详解与比较
- ORACLE replace和translate函数详解
- Oracle替换函数之replace和translate
- Oracle的Replace函数与translate函数详解与比较
- ORACLE replace和translate函数详解
- SQL 标量函数-----> 字符串函数 之四 字符串比较函数 CHARINDEX()、PATINDEX()、REPLICATE()、REVERSE()、REPLACE()、SPACE()、STUFF()
- 参数返回Oracle 常用函数:nvl/nullif/case when/wm_concat/replace
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace