首页 > 数据库 > Oracle > REPLACE & TRANSLATE
TRANSLATE provides single-character, one-to-one substitution. REPLACE lets you substitute one string for another as well as to remove character strings.
偷个懒,直接拷贝了Oracle的在线文档,嘿嘿!
[@more@]replace::=

REPLACE returns char with every occurrence of search_string replaced with replacement_string. If replacement_string is omitted or null, then all occurrences of search_string are removed. If search_string is null, then char is returned.
Both search_string and replacement_string, as well as char, can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char.
This function provides functionality related to that provided by the TRANSLATE function. TRANSLATE provides single-character, one-to-one substitution. REPLACE lets you substitute one string for another as well as to remove character strings.
| See Also: |
The following example replaces occurrences of "J" with "BL":
SELECT REPLACE('JACK and JUE','J','BL') "Changes"
FROM DUAL;
Changes
--------------
BLACK and BLUE
TRANSLATE
Syntax
translate::=
Text description of translatePurpose
TRANSLATEreturnscharwith all occurrences of each character infrom_stringreplaced by its corresponding character into_string. Characters incharthat are not infrom_stringare not replaced. The argumentfrom_stringcan contain more characters thanto_string. In this case, the extra characters at the end offrom_stringhave no corresponding characters into_string. If these extra characters appear inchar, then they are removed from the return value.You cannot use an empty string for
to_stringto remove all characters infrom_stringfrom the return value. Oracle interprets the empty string as null, and if this function has a null argument, then it returns null.
Note:This function does not support
CLOBdata directly. However,CLOBs can be passed in as arguments through implicit data conversion. Please refer to "Datatype Comparison Rules" for more information.Examples
The following statement translates a license number. All letters 'ABC...Z' are translated to 'X' and all digits '012 . . . 9' are translated to '9':
SELECT TRANSLATE('2KRW229', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "License" FROM DUAL; License -------- 9XXX999The following statement returns a license number with the characters removed and the digits remaining:
SELECT TRANSLATE('2KRW229', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789') "Translate example" FROM DUAL; Translate example ----------------- 2229
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7988176/viewspace-918948/ ,如需转载,请注明出处,否则将追究法律责任。