sql中的替换函数replace()总结
作者:mmseoamin日期:2023-12-13
1,表达式
--replace()
--语法:
REPLACE ( string_expression , string_pattern , string_replacement )
--参数:
string_expression:字符串表达式
string_pattern:想要查找的子字符串
string_replacement:想要替换成的子字符串
2、查询替换 
--将address字段里的 “区” 替换为 “呕” 显示,如下
 
select *,replace(address,'区','呕') AS rep
from test_tb
--ERP中我们对某一个字段中的数据进行去括号
YS = replace(replace(SCDDH.YSJ,'(',''),')','')
先用replace去左括号: replace(SCDDH.YSJ,'(','')
再用replace去右括号 )replace(SCDDH.YSJ,'(','')
 2、更新替换
将address字段里的 “东” 替换为 “西” ,如下
 
update test_tb set address=replace(address,'东','西') where id=2
 
总结:对字段中局部字符串做更新替换。
3、插入替换
将id=6的name字段值改为wokou
 
replace into test_tb VALUES(6,'wokou','新九州岛','日本')
 
总结:向表中“替换插入”一条数据,如果原表中没有id=6这条数据就作为新数据插入(相当于insert into作用);如果原表中有id=6这条数据就做替换(相当于update作用)。对于没有指定的字段以默认值插入。