用这个吧:
--创建数据测试环境
create table #tb(aa text)
insert into #tb values('abc123abc123,asd')
--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='123' --要替换的字符串
,@d_str='000' --替换成的字符串
--字符串替换处理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(aa),@rplen=len(@s_str),@postion=charindex(@s_str,aa)-1 from #tb
while @postion>0
begin
updatetext #tb.aa @p @postion @rplen @d_str
select @postion=charindex(@s_str,aa)-1 from #tb
end
--显示结果
select * from #tb
--删除数据测试环境
drop table #tb
--创建数据测试环境
create table #tb(aa text)
insert into #tb values('abc123abc123,asd')
--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='123' --要替换的字符串
,@d_str='000' --替换成的字符串
--字符串替换处理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(aa),@rplen=len(@s_str),@postion=charindex(@s_str,aa)-1 from #tb
while @postion>0
begin
updatetext #tb.aa @p @postion @rplen @d_str
select @postion=charindex(@s_str,aa)-1 from #tb
end
--显示结果
select * from #tb
--删除数据测试环境
drop table #tb
本文介绍了一个使用T-SQL在SQL Server中进行字符串替换的例子。通过创建临时表并插入特定数据,演示了如何循环查找并替换指定字符串的过程。
1630

被折叠的 条评论
为什么被折叠?



