大网数据广告活动
数据库部署与 SQL 参考

Apache Doris 中文参考:REPLACE

· 52CE 我爱测

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                          |
+------------------------------------+

本文来自 Apache Doris 文档贡献者,按 Apache 2.0 整理版式与相对链接,保留正文与代码。图示请参阅原文。作者与贡献记录。

资料来源与适用范围

资料来源
Apache Doris 文档贡献者
适用范围
master 文档快照(2026-11-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
Apache 2.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。