Sql Server REPLACE函数的使用
语法
REPLACE ( original-string, search-string, replace-string )
理解记忆
REPLACE("被搜索的字符串","被替换的字符串","替换的字符串")
参数
如果有某个参数为 NULL,此函数返回 NULL。
original-string : 被搜索的字符串。可为任意长度。
search-string : 要搜索并被 replace-string 替换的字符串。该字符串的长度不应超过 255 个字节。如果 search-string 是空字符串,则按原样返回原始字符串。
replace-string : 该字符串用于替换 search-string。可为任意长度。如果 replace-string 是空字符串,则删除出现的所有 search-string。
例子:
update [dbo].[Products] SET Content=replace(Content,'abc','ddd')
将表[dbo].[Products]中的Content字段中的 abc 替换为 ddd
这个函数有一点不足,它是不支持 text,ntext类型字段的替换,可以通过下面的语句来实现:
update [dbo].[Products] SET Content=replace(cast(Content AS varchar(8000)),'abc','ddd')