这个替换语句可以写吗?怎么写?在线等!!

unknow0 2005-01-04 11:59:00
现在想把数据库的prdt表的spc字段的某些文字,比如AAA全部替换成BBB,但是这个AAA的位置没有规律,这个想法可以实现吗? 语句要怎么写?

先谢过各位大虾了!
...全文
183 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lousidao 2005-01-10
  • 打赏
  • 举报
回复
up
henkuaile 2005-01-05
  • 打赏
  • 举报
回复
不管规不规则,以下代码都是可行的:
update prdt set spc = replace(spc,'AAA','BBB')
Softlee81307 2005-01-05
  • 打赏
  • 举报
回复
update prdt set spc=REPLACE(spc,'AAAA','BBBB')
631799 2005-01-05
  • 打赏
  • 举报
回复
test --表名
id --主键
txt --要替换的列(text类型)

只要把把相应的换掉你实际的就行了。

lxysjl 2005-01-05
  • 打赏
  • 举报
回复
接分
631799 2005-01-05
  • 打赏
  • 举报
回复
是的id 是主键,如果主键为aa 那用aa 替换id
ache7611 2005-01-05
  • 打赏
  • 举报
回复
以下应该可以实现﹕
update prdt set spc=REPLACE(spc,'AAAA','BBBB')
unknow0 2005-01-05
  • 打赏
  • 举报
回复
TO 631799(杭州工人)

spc字段是text类型。

我问的id是指from test where id=@id中的id,不是@id.
unknow0 2005-01-04
  • 打赏
  • 举报
回复
TO 631799(杭州工人)
不好意思,首先说一下我不会sql,所以问的问题可能幼稚了点,请见谅啊

你的语句中的id指的是什么?我理解为主键了不知道是不是。txt指的是我所说的spc字段吧
我按照我以上的理解把你提供的语句执行成功了,可是为什么我什么效果都没有看到呢?

因为prdt表的spc字段中有大量的纪录中都有AAA,所以我的意思是要批量改,但一般含此文字的纪录其主键是连续的,所以更改语句才能再限制的主键范围内更改呢?
谢谢!
july 2005-01-04
  • 打赏
  • 举报
回复
如果你修改
update your_tablename
set spc字段 = REPLACE(spc字段,'cde','xxx')
july 2005-01-04
  • 打赏
  • 举报
回复
在sql server中直接replace就可以了

回复人: goregrypeck(派克) ( ) 信誉:100 2005-01-04 12:06:00 得分: 0


SELECT REPLACE(spc字段,'cde','xxx') from your_tablename


631799 2005-01-04
  • 打赏
  • 举报
回复

如果是text类型:

--定义替换的字符串
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
tonny_yang 2005-01-04
  • 打赏
  • 举报
回复
declare @chvField nvarchar(200)
declare cur_test cursor for

select spc from prdt
open cur_test
fetch next from cur_test into @chvField
while @@fetch_status=0
begin
update prdt set spc=replace(@chvField ,'AAAA','BBBB') where spc=@chvField
fetch next from cur_test into @chvField
end
close cur_test
deallocate cur_test

前提是spc唯一,如果不唯一where后面可以用其他唯一字段代替
goregrypeck 2005-01-04
  • 打赏
  • 举报
回复
SELECT REPLACE(spc字段,'cde','xxx')
goregrypeck 2005-01-04
  • 打赏
  • 举报
回复
用 xxx 替换 abcdefghi 中的字符串 cde。

SELECT REPLACE('abcdefghicde','cde','xxx')
GO

cqdj 2005-01-04
  • 打赏
  • 举报
回复
可以实现,就是比较麻烦。
631799 2005-01-04
  • 打赏
  • 举报
回复
你的spc字段类型是什么类型的啊??
631799 2005-01-04
  • 打赏
  • 举报
回复
上面说的不对,只能一条一条记录的改
631799 2005-01-04
  • 打赏
  • 举报
回复
declare @id varchar(3) --这个ID是定义的变量啊
支持批量改的啊

我这种方法只适会字段类型为text型的,如果为其它类型,则用楼上其它的。

34,590

社区成员

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

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