语法格式:translate(expr, from_string, to_string)
示例如下:
select translate('ab你好bcadefg', 'abcdefg', '1234567') as new_str
from dual;
new_str
---------------
12 你好 2314567
from_string 与 to_string 以字符为单位,对应字符一一替换。
a | 1 | replace('a', '1') | 以字符为单位一一对应替换 |
b | 2 | replace('b', '2') | |
c | 3 | replace('c', '3') | |
d | 4 | replace('d', '4') | |
e | 5 | replace('e', '5') | |
f | 6 | replace('f', '6') | |
g | 7 | replace('g', '7') |
a | 1 | |
b | 2 | |
你 | 你 | 不作 替换 |
好 | 好 | |
b | 2 | 不管字符在何位置,均替换,如a、b |
c | 3 | |
a | 1 | |
d | 4 | |
e | 5 | |
f | 6 | |
g | 7 |
如果 to_string 为空,则返回空值。
SQL> set null NULL
Cannot SET NULL
SQL> col new_str from a10
SQL> select translate('ab你好bcadefg', 'abcdefg', '') as new_str from dual;
NEW_STR
-------------
NULL
如果 to_string 对应的位置没有字符,则 from_string 中列出的字符将会被消掉。
select translate('ab你好bcadefg', 'labcdefg', 'l') as new_str from dual;
NEW_STR
---------
你好
l | l | replace('l', 'l') |
a | 空 | replace('a', '') |
b | 空 | replace('b', '') |
c | 空 | replace('c', '') |
d | 空 | replace('d', '') |
e | 空 | replace('e', '') |
f | 空 | replace('f', '') |
g | 空 | replace('g', '') |
expr 中没有 l | l | 没有忽略 |
a | 空 | 同后面 |
b | 空 | |
你 | 你 | |
好 | 好 | |
b | 空 | 对应位置没有字符,替换为空 |
c | 空 | |
a | 空 | |
d | 空 | |
e | 空 | |
f | 空 | |
g | 空 |