微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Oracle Translate & Replace

点击打开链接

Oracle Translate & Replace
Version 11.1
Note:Translate and replace are very similar in their appearance but can produce very different results. Translate replaces by position,the first character of the list to match is replaced by the first character of the replacement list. The second character with the second,and if there are characters in the list to match that do not have positional equivalents in the replacements list they are dropped.

Replace replaces the string to match with the replacement string. The replacement of a single character is the same as that ofTRANSLATE.
Syntax TRANSLATE(
str1VARCHAR2CHaraCTER SET ANY_CS,
srcVARCHAR2CHaraCTER SET STR1%CHARSET,
destVARCHAR2CHaraCTER SET STR1%CHARSET)
RETURNVARCHAR2CHaraCTER SET STR1%CHARSET;
Translate Built-in String Function
Single Character
Replacement
TRANSLATE(<string>,<'list_to_match'>,<'replacements_list'>)

This demo replaces all commas with vertical bars.
SELECTTRANSLATE('comma,delimited,list',',','|')
FROMDUAL;
Multiple Character
Replacement
The following takes a DNA sequence and returns its complement
SELECTTRANSLATE('CAG-TTT-GAC-ACA-TGG-ATC','ACGT','GATC') DNA
FROMDUAL;
Character Replacement
And Elimination
The a is replaced with an e,the h has no complement and is dropped.
SELECTTRANSLATE('So What','ah','e')
FROMDUAL;
Eliminating Double
Quotes
Capital A is replaced with capital A. The double quote is eliminated because there is no match.
SELECTTRANSLATE('"Darn double quotes"','A"','A')
FROMDUAL;
Encryption / Decryption In this demo a string is first encrypted then decrypted
SELECTTRANSLATE('this is a secret',
'abcdefghijklmnopqrstuvxyz','0123456789qwertyuiop[kjhbv')
FROMDUAL;

SELECTTRANSLATE('p78o 8o 0 o42i4p',
'0123456789qwertyuiop[kjhbv','abcdefghijklmnopqrstuvxyz')
FROMDUAL;
Counting Vowels In this demo the number of vowels in the string is counted
WITH dataAS(SELECT'Whose line is it anyway' lineFROMDUAL)
SELECTLENGTH(line)-LENGTH(TRANSLATE(line,'xaeIoU','x')) nbVowels
FROMdata;
Replace Built-in String Function
REPLACE (overload 1) REPLACE(
srcstrVARCHAR2CHaraCTER SET ANY_CS,
oldsubVARCHAR2CHaraCTER SET SRCSTR%CHARSET,
newsubVARCHAR2CHaraCTER SET SRCSTR%CHARSET := NULL)
RETURNVARCHAR2CHaraCTER SET SRCSTR%CHARSET;
REPLACE (overload 2) REPLACE(
srcstrCLOBCHaraCTER SET ANY_CS,
oldsubCLOBCHaraCTER SET SRCSTR%CHARSET,
newsubCLOBCHaraCTER SET SRCSTR%CHARSET := NULL)
RETURNCLOBCHaraCTER SET SRCSTR%CHARSET;
Single Character
Replacement
REPLACE(<string>,<'string_to_match'>,<'replacements_string'>)
SELECTREPLACE('So What','o','ay')
FROMDUAL;
Multiple Character
Replacement
Replacement of a single character with a phrase
SELECTREPLACE('An ideathat is not dangerous is unworthy of being called an ideaat all.','n idea','software program') TRUTH
FROMDUAL;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐