--text字段替换处理示例
2005-03-16 12:30
344 查看
如果更新把ntext类型字段的内容更新成如"abcd"这样的串???
邹建 2004.07(引用请保留此信息)
--测试数据
create table tb(id int identity(1,1),content ntext)
insert tb select '001,002'
union all select '001,002,003,004,005,006,007,008,009,010'
go
--替换处理
declare @s_str nvarchar(4000),@r_str nvarchar(4000)
select @s_str='02' --要替换的字符串
,@r_str='**02' --替换成该字符串
--替换处理
declare @id int,@ptr varbinary(16)
declare @start int,@s nvarchar(4000),@len int
declare @s_str1 nvarchar(4000),@s_len int,@i int,@step int
select @s_str1=reverse(@s_str),@s_len=len(@s_str)
,@step=case when len(@r_str)>len(@s_str)
then 4000/len(@r_str)*len(@s_str)
else 4000 end
declare tb cursor local for
select id,start=charindex(@s_str,[content])-1
from [tb]
where id=2 --替换id=1的记录
and charindex(@s_str,[content])>0
open tb
fetch tb into @id,@start
while @@fetch_status=0
begin
select @ptr=textptr([content])
,@s=substring([content],@start+1,@step)
from [tb]
where id=@id
while len(@s)>=@s_len
begin
select @len=len(@s),@i=charindex(@s_str1,reverse(@s))
if @i>0
begin
select @i=case when @i>=@s_len then @s_len else @i end
,@s=replace(@s,@s_str,@r_str)
updatetext [tb].[content] @ptr @start @len @s
end
else
set @i=@s_len
select @start=@start+len(@s)-@i+1
,@s=substring([content],@start+1,@step)
from [tb]
where id=@id
end
fetch tb into @id,@start
end
close tb
deallocate tb
go
--显示处理结果
select * from tb
go
--删除测试
drop table tb
/*--测试结果
id content
----------- ----------------------------------------------
1 001,002
2 001,0**02,003,004,005,006,007,008,009,010
(所影响的行数为 2 行)
--*/
如果把整个字段的值替换为'abcd',则直接update
update 表 set ntext字段='abcd'
邹建 2004.07(引用请保留此信息)
--测试数据
create table tb(id int identity(1,1),content ntext)
insert tb select '001,002'
union all select '001,002,003,004,005,006,007,008,009,010'
go
--替换处理
declare @s_str nvarchar(4000),@r_str nvarchar(4000)
select @s_str='02' --要替换的字符串
,@r_str='**02' --替换成该字符串
--替换处理
declare @id int,@ptr varbinary(16)
declare @start int,@s nvarchar(4000),@len int
declare @s_str1 nvarchar(4000),@s_len int,@i int,@step int
select @s_str1=reverse(@s_str),@s_len=len(@s_str)
,@step=case when len(@r_str)>len(@s_str)
then 4000/len(@r_str)*len(@s_str)
else 4000 end
declare tb cursor local for
select id,start=charindex(@s_str,[content])-1
from [tb]
where id=2 --替换id=1的记录
and charindex(@s_str,[content])>0
open tb
fetch tb into @id,@start
while @@fetch_status=0
begin
select @ptr=textptr([content])
,@s=substring([content],@start+1,@step)
from [tb]
where id=@id
while len(@s)>=@s_len
begin
select @len=len(@s),@i=charindex(@s_str1,reverse(@s))
if @i>0
begin
select @i=case when @i>=@s_len then @s_len else @i end
,@s=replace(@s,@s_str,@r_str)
updatetext [tb].[content] @ptr @start @len @s
end
else
set @i=@s_len
select @start=@start+len(@s)-@i+1
,@s=substring([content],@start+1,@step)
from [tb]
where id=@id
end
fetch tb into @id,@start
end
close tb
deallocate tb
go
--显示处理结果
select * from tb
go
--删除测试
drop table tb
/*--测试结果
id content
----------- ----------------------------------------------
1 001,002
2 001,0**02,003,004,005,006,007,008,009,010
(所影响的行数为 2 行)
--*/
如果把整个字段的值替换为'abcd',则直接update
update 表 set ntext字段='abcd'
相关文章推荐
- SQL Server TEXT类型字段字符串替换示例处理脚本
- SQL Server TEXT类型字段字符串替换示例处理脚本
- --text字段的替换处理示例--全表替换
- text/ntext字段的替换处理--全表替换
- ntext字段的替换处理示例--全表替换
- text与ntext字段的复制和合并处理示例.sql
- text与image字段转换处理示例.sql
- ntext字段的替换处理示例--全表替换(作者:邹建)
- SQL text字段的替换处理(无法批量更新,写法记录)
- 字段名称Asp.Net中替换JSON中主键内容的函数(很简单,示例+说明)
- sql ,,replace替换数据类型为text的字段。。
- SQL查询中用replace替换ntext,text字段的各种方法总结
- 应用replace函数替换ntext/text/image 字段内容
- Sql Server 中 text或ntext 字段内容替换方法总结
- SqlServer Text类型字段超过8000字处理
- Sql Server 中 text或ntext,varchar,nvarchar 字段内容替换方法总结z
- sql IN 的用法一例--替换 mysql longtext字段中某些内容的用法
- 数据库ntext,text字段的内容批量替换储存过程 转
- SQL text类型字段字符替换
- 推荐一个text或ntext 字段内容替换方法