tb_a
col1 col2 col3
1 aa header abcd[desc]ad3 333 [add]0
2 bb sdfasdfasdfs[desc] add
3 aa header 7788-02-3 [idesc]zhongguo beijing
4 aa header 00909090 [desc]sdf [upd]dfaas
5 aa header sdfasdf [idesc]yy [bade]dafdf
6 aa header adsd890s8098-08 [desc]9 sfjasjsdff修改后的表为:
col1 col2 col3
1 aa header abcd[idesc]ad3 333 [add]0
2 bb sdfasdfasdfs[desc] add
3 aa header 7788-02-3 [idesc]zhongguo beijing
4 aa header 00909090 [idesc]sdf [upd]dfaas
5 aa header sdfasdf [idesc]yy [bade]dafdf
6 aa header adsd890s8098-08 [idesc]9 sfjasjsdff在tb_a表中 ,col1 是主键.
当col2 =aa的时候,只修改col3 列中[desc] --->[idesc]
请问用sql语句能实现吗?
col1 col2 col3
1 aa header abcd[desc]ad3 333 [add]0
2 bb sdfasdfasdfs[desc] add
3 aa header 7788-02-3 [idesc]zhongguo beijing
4 aa header 00909090 [desc]sdf [upd]dfaas
5 aa header sdfasdf [idesc]yy [bade]dafdf
6 aa header adsd890s8098-08 [desc]9 sfjasjsdff修改后的表为:
col1 col2 col3
1 aa header abcd[idesc]ad3 333 [add]0
2 bb sdfasdfasdfs[desc] add
3 aa header 7788-02-3 [idesc]zhongguo beijing
4 aa header 00909090 [idesc]sdf [upd]dfaas
5 aa header sdfasdf [idesc]yy [bade]dafdf
6 aa header adsd890s8098-08 [idesc]9 sfjasjsdff在tb_a表中 ,col1 是主键.
当col2 =aa的时候,只修改col3 列中[desc] --->[idesc]
请问用sql语句能实现吗?
解决方案 »
- ???简单SQL语句,如何查询下面结果???
- 改进一条sql语句
- 关于将excel导入sql数据库的问题
- windows验证和SQLSERVER验证有什么区别?
- 两个不同时间的数据库文件(MDF,LDF都有)能不能恢复到他们中间的时间点????????
- CREATE/alter TABLE如何给字段写说明,并非SQL注释--和/* */,而是给一字段的说明,要求可以在企业管理器中查阅到.
- 数据库连接时而缓慢、时而迅速,补丁都打其了,仍然不得头绪,各位大虾帮忙。
- 想学习SQL语句,出报表,请问上哪弄点参考资料或者例子呢?
- 14,000,000条纪录的一个大表,应该怎么处理以提高速度?
- SOS ---求救!!!!!!!--SQLServer???
- 求一条sql语句08年和09年工资汇总
- 如何在cs页面中调用一个sql 函数?
update tb_a
set col3=replace(col3,'[desc]','[idesc]')
where col2='aa'
友情提醒,更新之前请先做备份
这个好像不行,因为我的字段是 text 类型? 提示该类型不能用Replace
update test set test.content =test1.b
from test, (
select id as a,REPLACE(CAST( content as varchar),'日本','日本鬼子')as b from test
) test1
where test.id =test1.a
--邹建 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 行) --*/