如何替换ntext类型的数据?

xiaoshushifu 2004-07-18 02:52:45
如何替换一个数据表里ntext类型的列的数据,
如把'aaa'替换成'bbb',
请高手指点!谢谢!

我试了一下UPDATETEXT, 但必须事先知道被替换字符串的起始位置,感觉不适用。
...全文
188 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
雨青 2005-05-30
  • 打赏
  • 举报
回复
REPLACE(cast (isnull(字段名,'') as nvarchar(4000)),' ','')
posonhuang 2005-05-30
  • 打赏
  • 举报
回复
study
ljial 2004-12-06
  • 打赏
  • 举报
回复
ntext是可以update的!!
yuekai 2004-12-06
  • 打赏
  • 举报
回复
这个运行起来要好长时间啊,能不能提高一些效率呢?
---涛声依旧--- 2004-07-19
  • 打赏
  • 举报
回复
to:一樓的
在ntext类型中能不能用REPLACE
---涛声依旧--- 2004-07-19
  • 打赏
  • 举报
回复
學習
zjcxc 元老 2004-07-19
  • 打赏
  • 举报
回复
--text字段的替换处理示例--全表替换

--创建数据测试环境
create table test(id varchar(3),txt text)
insert into test
select '001',REPLICATE('a',4000) + REPLICATE('b',2000) + REPLICATE('ab',2000)
union all select '002',REPLICATE('a',4000) + REPLICATE('b',2000) + REPLICATE('ab',2000)
go
select datalength(txt),* from test

--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='a' --要替换的字符串
,@d_str='ab' --替换成的字符串


--因为只能用patindex,所以对于搜索字符串做处理
set @s_str='%'+@s_str+'%'

--定义游标,循环处理数据
declare @id varchar(3)
declare #tb cursor for select id from test
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16)
,@p1 int,@p2 int
,@rplen int,@step int,@len int

select @p=textptr(txt)
,@rplen=len(@s_str)-2
,@step=len(@d_str)
,@p1=patindex(@s_str,txt)
,@len=datalength(txt)
,@p2=0
from test where id=@id

while @p1>0
begin
set @p2=@p1+@p2-1
updatetext test.txt @p @p2 @rplen @d_str
select @p2=@p2+1,@p1=patindex(@s_str,substring(txt,@p2+1,@len))
from test where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb

--显示结果
select datalength(txt),* from test

go
--删除数据测试环境
drop table test
JohnShen 2004-07-19
  • 打赏
  • 举报
回复
试试
REPLACE ( 'string_expression1' , 'string_expression2' , 'string_expression3' )
不过,我不知道在ntext类型中能不能用

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧