表名:hello
字段:CONTENT
content字段中的内容:
1<img align="baseline" alt="" border="0" src="http://localhost/upimg/0335_p1.jpg"/>
1<br/>
……
结果:想把的'http://localhost/'去掉
记录很多,怎么写SQL语句啊
---------------------------------------------------------------
SQL中可以用类似下面的方法处理:
--text处理示例:
create table hello(id int identity(1,1),CONTENT text)
insert into hello
select '
1<img align="baseline" alt="" border="0" src="http://localhost/upimg/0335_p1.jpg"/>
1<br/>
'
union all select '
1<img align="baseline" alt="" border="0" src="http://localhost/upimg/0335_p1.jpg"/>
1<br/>
'
--定义替换/删除的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='http://localhost/' --要替换的字符串
,@d_str='' --替换成的字符串
--定义游标,循环处理数据
declare @id int
declare #tb cursor for select id from hello
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr(CONTENT),@rplen=len(@s_str),@postion=charindex(@s_str,CONTENT)-1 from hello where id=@id
while @postion>0
begin
updatetext hello.CONTENT @p @postion @rplen @d_str
select @postion=charindex(@s_str,CONTENT)-1 from hello where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb
--显示结果
select * from hello
--删除数据测试环境
drop table hello