SQL 中 REPLACE 函数用于在一个文本或字符串中替换所有给定的子字符串为另一个子字符串。可用于基本替换、条件替换和 NULL 值处理。基本语法为:REPLACE(string, old_substring, new_substring)。
SQL 中 REPLACE 函数的用法
REPLACE 函数用于在一个文本或字符串中替换所有给定的子字符串为另一个子字符串。它可以根据指定的条件更新数据库中的数据。
语法
REPLACE(string, old_substring, new_substring)
参数
用法
SELECT REPLACE('This is a sample text', 'sample', 'example');结果:This is an example text
SELECT REPLACE('This is a sample text', 'sample', 'example', 1);结果:This is an example text
其中,1 指定只替换第一个出现的子字符串。
如果 old_substring 或 new_substring 为 NULL,则返回 NULL。SELECT REPLACE('This is a sample text', NULL, 'example');结果:NULL
注意事项