34,575
社区成员
发帖
与我相关
我的任务
分享
declare @pos int
declare @len int
declare @str nvarchar(4000)
declare @des nvarchar(4000)
declare @count int
set @des ='a'--要替换的字符
set @len=len(@des)
set @str= 'b'--要替换成的值
set @count=0--统计次数.
WHILE 1=1
BEGIN
select @pos=patINDEX('%'+@des+'%',txt) - 1
from test --表名
where txt is not null
IF @pos>=0 -- 如果找到 要替换的字符 字符就执行下面的语句
begin
DECLARE @ptrval binary(16)
SELECT @ptrval = TEXTPTR(txt)
from test --表名
where txt is not null --条件
UPDATETEXT test.txt @ptrval @pos @len @str
set @count=@count+1
end
ELSE
break;
END
select @count
--这样就行了吧?
[code=SQL]
[code=SQL]
--kkkk
[/code]create table test(txt nvarchar(100))
insert into test select 'kloweakdnalkaKD'
insert into test select 'onmsadjodaiad'
insert into test select 'vwioaujh'
insert into test select 'foimnsd'
go
declare @pos int
declare @len int
declare @str nvarchar(4000)
declare @des nvarchar(4000)
declare @count int
set @des ='a'--要替换的字符
set @len=len(@des)
set @str= 'b'--要替换成的值
set @count=0--统计次数.
while exists(select 1 from test where charindex(@des,txt)>0)
begin
update test set txt=stuff(txt,charindex(@des,txt),@len,@str) from test where charindex(@des,txt)>0
set @count=@count+@@ROWCOUNT
end
select @count
select * from test
go
drop table test
/*
-----------
7
(1 行受影响)
txt
----------------------------------------------------------------------------------------------------
klowebkdnblkbKD
onmsbdjodbibd
vwiobujh
foimnsd
(4 行受影响)
*/
/*
WHILE 1=1
BEGIN
select @pos=patINDEX('%'+@des+'%',txt) - 1
from test --表名
where txt is not null
IF @pos>=0 -- 如果找到 要替换的字符 字符就执行下面的语句
begin
DECLARE @ptrval binary(16)
SELECT @ptrval = TEXTPTR(txt)
from test --表名
where txt is not null --条件
UPDATETEXT test.txt @ptrval @pos @len @str
set @count=@count+1
end
ELSE
break;
END
select @count
*/