sql server replace 的使用方法

Sql Server REPLACE函数的使用

 

REPLACE
用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式。

语法
REPLACE ( ''string_replace1'' , ''string_replace2'' , ''string_replace3'' )

参数
''string_replace1''

待搜索的字符串表达式。string_replace1 可以是字符数据或二进制数据。

''string_replace2''

待查找的字符串表达式。string_replace2 可以是字符数据或二进制数据。

''string_replace3''

替换用的字符串表达式。string_replace3 可以是字符数据或二进制数据。

返回类型
如果 string_replace(1、2 或 3)是支持的字符数据类型之一,则返回字符数据。如果 string_replace(1、2 或 3)是支持的 binary 数据类型之一,则返回二进制数据。

示例
下例用 xxx 替换 abcdefghi 中的字符串 cde。

SELECT REPLACE(''abcdefghicde'',''cde'',''xxx'')GO
下面是结果集:

------------abxxxfghixxx(1 row(s) affected)

还有从某一列的值中替换的功能

demo 如下

create table #table (
id int PRIMARY  key IDENTITY(1,1),
msg VARCHAR(30)



)
INSERT into #table (msg) VALUES('a')
INSERT into #table (msg) VALUES('b')
INSERT into #table (msg) VALUES('c')
INSERT into #table (msg) VALUES('d')
INSERT into #table (msg) VALUES('e')
INSERT into #table (msg) VALUES('f')




update #table set msg =REPLACE(msg, 'c','aa')


SELECT *from #table



原文地址:https://www.cnblogs.com/baili-luoyun/p/11150738.html