跳到主要内容

REPLACE

描述

REPLACE 函数用于将字符串中所有出现的指定子字符串替换为新的子字符串。该函数会替换字符串中所有匹配的子字符串实例,执行全局替换操作。

与 REPLACE_EMPTY 函数的区别:

  • REPLACE() 替换指定的子字符串,包括空字符串
  • REPLACE_EMPTY() 专门用于将空值或空字符串替换为指定值

语法

REPLACE(<str>, <old>, <new>)

参数

参数说明
<str>需要进行替换操作的源字符串。类型:VARCHAR
<old>要被替换的目标子字符串。如果在 str 中不存在,则不进行替换。类型:VARCHAR
<new>用于替换 old 的新子字符串。类型:VARCHAR

返回值

返回 VARCHAR 类型,表示替换操作后的新字符串。

替换规则:

  • 替换字符串中所有匹配的 old 子字符串
  • 替换是大小写敏感的
  • 如果 old 为空字符串,返回原字符串(不进行任何操作)
  • 如果 new 为空字符串,相当于删除所有匹配的 old 子字符串

特殊情况:

  • 如果任意参数为 NULL,返回 NULL
  • 如果 str 为空字符串,返回空字符串
  • 如果 old 为空字符串,返回原 str(不进行替换)
  • 如果 old 在 str 中不存在,返回原 str

示例

  1. 基本替换操作
SELECT REPLACE('hello world', 'world', 'universe');
+---------------------------------------------+
| REPLACE('hello world', 'world', 'universe') |
+---------------------------------------------+
| hello universe |
+---------------------------------------------+
  1. 替换多个匹配项
SELECT REPLACE('apple apple apple', 'apple', 'orange');
+------------------------------------------------+
| REPLACE('apple apple apple', 'apple', 'orange') |
+------------------------------------------------+
| orange orange orange |
+------------------------------------------------+
  1. 删除子字符串(替换为空字符串)
SELECT REPLACE('banana', 'a', '');
+---------------------------+
| REPLACE('banana', 'a', '') |
+---------------------------+
| bnn |
+---------------------------+
  1. NULL 值处理
SELECT REPLACE(NULL, 'old', 'new'), REPLACE('test', NULL, 'new'), REPLACE('test', 'old', NULL);
+------------------------------+------------------------------+------------------------------+
| REPLACE(NULL, 'old', 'new') | REPLACE('test', NULL, 'new') | REPLACE('test', 'old', NULL) |
+------------------------------+------------------------------+------------------------------+
| NULL | NULL | NULL |
+------------------------------+------------------------------+------------------------------+
  1. 空字符串处理
SELECT REPLACE('', 'old', 'new'), REPLACE('test', '', 'new'), REPLACE('test', 'old', '');
+---------------------------+---------------------------+---------------------------+
| REPLACE('', 'old', 'new') | REPLACE('test', '', 'new') | REPLACE('test', 'old', '') |
+---------------------------+---------------------------+---------------------------+
| | test | test |
+---------------------------+---------------------------+---------------------------+
  1. 大小写敏感性
SELECT REPLACE('Hello HELLO hello', 'hello', 'hi');
+----------------------------------------------+
| REPLACE('Hello HELLO hello', 'hello', 'hi') |
+----------------------------------------------+
| Hello HELLO hi |
+----------------------------------------------+
  1. 子字符串不存在
SELECT REPLACE('hello world', 'xyz', 'abc');
+--------------------------------------+
| REPLACE('hello world', 'xyz', 'abc') |
+--------------------------------------+
| hello world |
+--------------------------------------+
  1. UTF-8 字符替换
SELECT REPLACE('ṭṛì ḍḍumai test ṭṛì ḍḍumannàri', 'ṭṛì', 'replaced');
+-----------------------------------------------------------+
| REPLACE('ṭṛì ḍḍumai test ṭṛì ḍḍumannàri', 'ṭṛì', 'replaced') |
+-----------------------------------------------------------+
| replaced ḍḍumai test replaced ḍḍumannàri |
+-----------------------------------------------------------+
  1. 数字字符串替换
SELECT REPLACE('123123123', '123', 'ABC');
+------------------------------------+
| REPLACE('123123123', '123', 'ABC') |
+------------------------------------+
| ABCABCABC |
+------------------------------------+