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

Oracle 函数replace和translate的比较

2009-06-02 15:40 471 查看
今天看了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

[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'

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