Oracle 使用TRANSLATE函数替换字符串中的字符
2013-04-25 10:06
507 查看
一个需求,如何将(1234,56789,12345)替换为'1234*56789*12345'?
第一种方式,使用replace:
ChenZw> SELECT REPLACE(REPLACE(REPLACE('(1234,56789,12345)',',','*'),'(',''''),')','''') FROM DUAL;
REPLACE(REPLACE(RE
------------------
'1234*56789*12345'
已选择 1 行。
上面的代码明显比较粗鲁,下面看一下Replace函数的官方介绍:
Purpose
with
or null, then all occurrences of
null, then
Both
can be any of the data types
The string returned is in the same character set as
and returns
Examples
The following example replaces occurrences of
第二种方式,使用translate函数:
ChenZw> SELECT TRANSLATE('(1234,56789,12345)','(,)','''*''') AS STR FROM DUAL;
STR
------------------
'1234*56789*12345'
已选择 1 行。
使用这个函数处理这个问题就看起来比较舒服一点,至少没有replace那么暴力,看一下官方关于translate函数的介绍:
Purpose
by its corresponding character in
are not in
contain more characters than
no corresponding characters in
then they are removed from the return value.
If a character appears multiple times in
corresponding to the first occurrence is used.
You cannot use an empty string for
the return value. Oracle Database interprets the empty string as null, and if this function has a null argument, then it returns null. To remove all characters in
concatenate another character to the beginning of
For example,
removes all digits from
as well as remove character strings.
This function does not support
Examples
The following statement translates a book title into a string that could be used (for example) as a filename. The
asterisk, slash, and apostrophe (with an extra apostrophe as the escape character). The
in the
作者 陈字文(热衷于PM\ORACLE\J***A等,欢迎同行交流):ziwen#163.com 扣扣:4零9零2零1零零
第一种方式,使用replace:
ChenZw> SELECT REPLACE(REPLACE(REPLACE('(1234,56789,12345)',',','*'),'(',''''),')','''') FROM DUAL;
REPLACE(REPLACE(RE
------------------
'1234*56789*12345'
已选择 1 行。
上面的代码明显比较粗鲁,下面看一下Replace函数的官方介绍:
Purpose
REPLACEreturns
charwith every occurrence of
search_stringreplaced
with
replacement_string. If
replacement_stringis omitted
or null, then all occurrences of
search_stringare removed. If
search_stringis
null, then
charis returned.
Both
search_stringand
replacement_string, as well as
char,
can be any of the data types
CHAR,
VARCHAR2,
NCHAR,
NVARCHAR2,
CLOB, or
NCLOB.
The string returned is in the same character set as
char. The function returns
VARCHAR2if the first argument is not a LOB
and returns
CLOBif the first argument is a LOB.
Examples
The following example replaces occurrences of
Jwith
BL:
SELECT REPLACE('JACK and JUE','J','BL') "Changes" FROM DUAL; Changes -------------- BLACK and BLUE
第二种方式,使用translate函数:
ChenZw> SELECT TRANSLATE('(1234,56789,12345)','(,)','''*''') AS STR FROM DUAL;
STR
------------------
'1234*56789*12345'
已选择 1 行。
使用这个函数处理这个问题就看起来比较舒服一点,至少没有replace那么暴力,看一下官方关于translate函数的介绍:
Purpose
TRANSLATEreturns
exprwith all occurrences of each character in
from_stringreplaced
by its corresponding character in
to_string. Characters in
exprthat
are not in
from_stringare not replaced. The argument
from_stringcan
contain more characters than
to_string. In this case, the extra characters at the end of
from_stringhave
no corresponding characters in
to_string. If these extra characters appear in
expr,
then they are removed from the return value.
If a character appears multiple times in
from_string, then the
to_stringmapping
corresponding to the first occurrence is used.
You cannot use an empty string for
to_stringto remove all characters in
from_stringfrom
the return value. Oracle Database interprets the empty string as null, and if this function has a null argument, then it returns null. To remove all characters in
from_string,
concatenate another character to the beginning of
from_stringand specify this character as the
to_string.
For example,
TRANSLATE(
expr, '
x0123456789', '
x')
removes all digits from
expr.
TRANSLATEprovides functionality related to that provided by the
REPLACEfunction.
REPLACElets you substitute a single string for another single string,
as well as remove character strings.
TRANSLATElets you make several single-character, one-to-one substitutions in one operation.
This function does not support
CLOBdata directly. However,
CLOBs can be passed in as arguments through implicit data conversion.
Examples
The following statement translates a book title into a string that could be used (for example) as a filename. The
from_stringcontains four characters: a space,
asterisk, slash, and apostrophe (with an extra apostrophe as the escape character). The
to_stringcontains only three underscores. This leaves the fourth character
in the
from_stringwithout a corresponding replacement, so apostrophes are dropped from the returned value.
SELECT TRANSLATE('SQL*Plus User''s Guide', ' */''', '___') FROM DUAL; TRANSLATE('SQL*PLUSU -------------------- SQL_Plus_Users_Guide
作者 陈字文(热衷于PM\ORACLE\J***A等,欢迎同行交流):ziwen#163.com 扣扣:4零9零2零1零零
相关文章推荐
- Oracle的字符替换函数translate用法
- oracle的常用函数 instr() 和substr()函数 博客分类: oracle 在Oracle中 可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符。 在一个
- Oracle 函数 Translate 的用法 (可用于统计 某字符 在字符串中 出现的次数)
- str_replace() 函数使用一个字符串替换字符串中的另一些字符
- 【字符串处理函数】遇到某个字符就使用另外两个字符替换
- javascript中使用replaceAll()函数实现字符替换的方法
- 禁止使用的字符操作相关系统函数级替换函数
- 使用正则表达式替换字符串中的字符
- 字符串替换空格:请实现一个函数,把字符数组中的每个空格替换成“%20”
- dede调用数据时,字符串替换函数使用
- 学python(03)—— 如何使用函数实现一个随机字符串里的大小写字符互换
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- 用 Replace 函数对字符串内某些字符进行替换
- ORACLE中的字符串替换 replce、regexp_replace 和 translate
- oracle 字符串处理instr()函数使用
- 编写一个函数reverse_string(char * string)(递归实现)实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- JS字符串替换函数全部替换方法 JS 正则表达式如何使用变量
- 把参数字符串中的字符反向排列(不使用数组和库函数操作字符串的函数,不要局部数组来临时存储参数字符串)
- oracle中去除字符串两边的空格和,替换,截取字符串的函数
- Oracle select --字符函数:TRANSLATE(string,from_str,to_str)