REPLACE(s, s1, s2) 替换函数使用字符串 s2 替换字符串 s 中所有的字符串 s1。如果 REPLACE 三个参数中任何一个为 NULL,则直接输出 NULL。
(1)使用 REPLACE 函数将 “0123456789” 字符串中的 “3456” 子字符串替换成 “hello” 字符串。如下:
mysql> select REPLACE('0123456789', '3456', 'hello'); +----------------------------------------+ | REPLACE('0123456789', '3456', 'hello') | +----------------------------------------+ | 012hello789 | +----------------------------------------+ 1 row in set (0.02 sec)
(2)使用 REPLACE 函数将 “01234567890101” 字符串中的所有子字符串 “0” 替换成 “#” 字符串。如下:
mysql> select REPLACE('01234567890101', '0', '#'); +-------------------------------------+ | REPLACE('01234567890101', '0', '#') | +-------------------------------------+ | #123456789#1#1 | +-------------------------------------+ 1 row in set (0.02 sec)
(3)如果 REPLACE 函数参数为 NULL,则直接输出 NULL。如下:
mysql> select REPLACE(null, '3456', 'hello'); +--------------------------------+ | REPLACE(null, '3456', 'hello') | +--------------------------------+ | NULL | +--------------------------------+ 1 row in set (0.02 sec) mysql> select REPLACE('0123456789', NULL, 'hello'); +--------------------------------------+ | REPLACE('0123456789', NULL, 'hello') | +--------------------------------------+ | NULL | +--------------------------------------+ 1 row in set (0.03 sec) mysql> select REPLACE('01234567890101', '3456', NULL); +-----------------------------------------+ | REPLACE('01234567890101', '3456', NULL) | +-----------------------------------------+ | NULL | +-----------------------------------------+ 1 row in set (0.03 sec)