您的位置:首页 > 数据库 > Oracle

Oracle 函数replace和translate的比较

2008-11-06 16:17 351 查看
今天看了SQLCOOKBOOK中的一个例子,其中看到了两个函数Replace和Translate时产生了疑惑,感觉这两个函数的作用是一样的,书上面的例子也看的不是很明白,Google了一下,看了Oracle的官方解释,终于彻底明白了。官方的解释如下:

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

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]
TRANSLATE
providesfunctionalityrelatedtothatprovidedbythe
REPLACE
function.
REPLACE
letsyousubstituteasinglestringforanothersinglestring,aswellasremovecharacterstrings.
TRANSLATE
letsyoumakeseveralsingle-character,one-to-onesubstitutionsinoneoperation.[/code]
Thisfunctiondoesnotsupport[code]CLOB
datadirectly.However,
CLOB
scanbepassedinasargumentsthroughimplicitdataconversion.[/code]

SeeAlso:

"DatatypeComparisonRules"formoreinformationandREPLACE

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: