MySQL REPLACE函数


本MySQL教程通过语法和示例说明了如何使用MySQL REPLACE函数。

描述

MySQL REPLACE函数用于替换字符串中出现的所有子字符串。

语法

MySQL中REPLACE函数的语法为:

1
REPLACE( string, from_substring, to_substring )
参数 说明
string 在哪个字符串。
from_substring 要查找的子字符串。在string中找到的所有from_substring都将被替换为to_substring。
to_substring 替换子字符串。在string中找到的所有from_substring都替换为to_substring。

注意REPLACE函数查找时是区分大小写的。

示例

如何在MySQL中使用REPLACE函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mysql> SELECT REPLACE('igiftidea.org', 'igiftidea', 'ykbj');
Result: 'ykbj.org'
mysql> SELECT REPLACE('igiftidea.com', 'igiftidea', 'ykbj');
Result: 'igiftidea.com'
mysql> SELECT REPLACE('abc abc', 'a', 'A');
Result: 'Abc Abc'
-- 注意区分大小写
mysql> SELECT REPLACE('abc abc', 'A', 'B');
Result: 'abc abc'
mysql> SELECT REPLACE('123 123', 2, 5);
Result: '153 153'
mysql> SELECT REPLACE('1223 1223', 2, 5);
Result: '1553 1553'
-- 数值都会先转换为字符串再查找
mysql> SELECT REPLACE('12.3 12.3', 2.3, 5.2);
Result: '15.2 15.2'


原文链接:https://codingdict.com/