Oracle 函数replace和translate的比较
2009-06-02 15:40
471 查看
今天看了SQLCOOKBOOK中的一个例子,其中看到了两个函数Replace和Translate时产生了疑惑,感觉这两个函数的作用是一样的,书上面的例子也看的不是很明白,Google了一下,看了Oracle的官方解释,终于彻底明白了。官方的解释如下:
REPLACE
Descriptionoftheillustrationreplace.gif
Purpose
returns
witheveryoccurrenceof
replacedwith
.If
isomittedornull,thenalloccurrencesof
areremoved.If
isnull,then
isreturned.
Both
and
,aswellas
,canbeanyofthedatatypes
,
,
,
,
,or
.Thestringreturnedisinthesamecharactersetas
.Thefunctionreturns
ifthefirstargumentisnotaLOBandreturns
ifthefirstargumentisaLOB.
providesfunctionalityrelatedtothatprovidedbythe
function.
providessingle-character,one-to-onesubstitution.
letsyousubstituteonestringforanotheraswellastoremovecharacterstrings.
SeeAlso:
TRANSLATE
Examples
Thefollowingexamplereplacesoccurrencesof
with
:
returns
withalloccurrencesofeachcharacterin
replacedbyitscorrespondingcharacterin
.Charactersin
thatarenotin
arenotreplaced.If
isacharacterstring,thenyoumustencloseitinsinglequotationmarks.Theargument
cancontainmorecharactersthan
.Inthiscase,theextracharactersattheendof
havenocorrespondingcharactersin
.Iftheseextracharactersappearin
,thentheyareremovedfromthereturnvalue.
[/code]
toremoveallcharactersin
fromthereturnvalue.OracleDatabaseinterpretstheemptystringasnull,andifthisfunctionhasanullargument,thenitreturnsnull.
[/code]
providesfunctionalityrelatedtothatprovidedbythe
function.
letsyousubstituteasinglestringforanothersinglestring,aswellasremovecharacterstrings.
letsyoumakeseveralsingle-character,one-to-onesubstitutionsinoneoperation.
[/code]
datadirectly.However,
scanbepassedinasargumentsthroughimplicitdataconversion.
[/code]
containsfourcharacters:aspace,asterisk,slash,andapostrophe(withanextraapostropheastheescapecharacter).The
containsonlythreeunderscores.Thisleavesthefourthcharacterinthe
withoutacorrespondingreplacement,soapostrophesaredroppedfromthereturnedvalue.
[/code]
REPLACE
Descriptionoftheillustrationreplace.gif
Purpose
REPLACE
returns
char
witheveryoccurrenceof
search_string
replacedwith
replacement_string
.If
replacement_string
isomittedornull,thenalloccurrencesof
search_string
areremoved.If
search_string
isnull,then
char
isreturned.
Both
search_string
and
replacement_string
,aswellas
char
,canbeanyofthedatatypes
CHAR
,
VARCHAR2
,
NCHAR
,
NVARCHAR2
,
CLOB
,or
NCLOB
.Thestringreturnedisinthesamecharactersetas
char
.Thefunctionreturns
VARCHAR2
ifthefirstargumentisnotaLOBandreturns
CLOB
ifthefirstargumentisaLOB.
REPLACE
providesfunctionalityrelatedtothatprovidedbythe
TRANSLATE
function.
TRANSLATE
providessingle-character,one-to-onesubstitution.
REPLACE
letsyousubstituteonestringforanotheraswellastoremovecharacterstrings.
SeeAlso:
TRANSLATE
Examples
Thefollowingexamplereplacesoccurrencesof
J
with
BL
:
SELECTREPLACE('JACKandJUE','J','BL')"Changes" FROMDUAL; Changes -------------- BLACKandBLUE
Translate
Descriptionoftheillustrationtranslate.gif
Purpose
[code]TRANSLATE
returns
expr
withalloccurrencesofeachcharacterin
from_string
replacedbyitscorrespondingcharacterin
to_string
.Charactersin
expr
thatarenotin
from_string
arenotreplaced.If
expr
isacharacterstring,thenyoumustencloseitinsinglequotationmarks.Theargument
from_string
cancontainmorecharactersthan
to_string
.Inthiscase,theextracharactersattheendof
from_string
havenocorrespondingcharactersin
to_string
.Iftheseextracharactersappearin
char
,thentheyareremovedfromthereturnvalue.
[/code]
Youcannotuseanemptystringfor[code]to_string
toremoveallcharactersin
from_string
fromthereturnvalue.OracleDatabaseinterpretstheemptystringasnull,andifthisfunctionhasanullargument,thenitreturnsnull.
[/code]
[code]TRANSLATE
providesfunctionalityrelatedtothatprovidedbythe
REPLACE
function.
REPLACE
letsyousubstituteasinglestringforanothersinglestring,aswellasremovecharacterstrings.
TRANSLATE
letsyoumakeseveralsingle-character,one-to-onesubstitutionsinoneoperation.
[/code]
Thisfunctiondoesnotsupport[code]CLOB
datadirectly.However,
CLOB
scanbepassedinasargumentsthroughimplicitdataconversion.
[/code]
SeeAlso:
"DatatypeComparisonRules"
formoreinformationand
REPLACE
Examples
Thefollowingstatementtranslatesabooktitleintoastringthatcouldbeused(forexample)asafilename.The[code]from_string
containsfourcharacters:aspace,asterisk,slash,andapostrophe(withanextraapostropheastheescapecharacter).The
to_string
containsonlythreeunderscores.Thisleavesthefourthcharacterinthe
from_string
withoutacorrespondingreplacement,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
SQLCookBook上這個例子分解
selecttranslate('SMITH20','0123456789','##########')fromdual將
'SMITH20'
字符串中,所有數字替換為‘#’
結果是
'SMITH##'
selectreplace(translate('SMITH20','0123456789','##########'),'#','')fromdual將
'SMITH##'
中的'#'替換為''
結果是'SMITH'
相关文章推荐
- 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中replace与translate
- MySQL与Oracle 差异比较之三函数