简要比较:
函数
|
作用
|
效果
|
REPLACE('aabd','ab','66')
|
字符串级别的代替
|
a66b
|
translate
('aabd','ab','66')
|
字符级别的代替
|
6666
|
分别详解
replace:
如果replacement_string为空或为null,那么所有的search_string都被移除
select replace('acdd','cd','') from dual; --> ad
如果search_string 为null,那么就返回原来的char
select replace('acdd','ef') from dual; -->acdd
select replace('acdd','','') from dual; -->acdd(也是两者都为空的情况)
translate:
translate中有“#”的特殊用法,以#开头的表示所有字符
例:
select translate('zhongjie4z123he','#zhongjiezhe','#') from dual;--4123
select translate('zhongjie4z123he','#zhongjiezhe','#z') from dual;--z4z123
select translate('zhongjie4z123he','#zhongjiezhe','#zx') from dual;--zx4z123x
select translate('asadad434323', '#0123456789','#') from dual;--asadad
补充:
1、转换源字串(from)在目的字串(to)中不存在对应,则转换后被截除2、转换目的字串(to)不能为'',''在oracle中被视为空值,因此无法匹配而返回为空值
3、一个汉字作为一个字符还是两个字符进行转换与字符集的设置相关。