日期:2014-05-19  浏览次数:20483 次

求一简单的sql语句!!
t1表中有一字段   a
a(列名)
123/234234
123/afsdf
123/oi23#
123/89324
123/@#$
123/2323
想在表内容改成
a(列名)
1234/234234
1234/afsdf
1234/oi23#
1234/89324
1234/@#$
1234/2323

就是 "/ "前面的123改成1234   "/ "后面的数据不变

求此sql语句

------解决方案--------------------
不过你要小心,这样是替换了所有的123为1234
------解决方案--------------------
UPDATE [t1]
SET [bbb] = stuff([bbb],1,3, '1234 ')
------解决方案--------------------
请注意第二个参数即要替换字符串的开始位置,索引不是从0开始,而是从自然数1开始
------解决方案--------------------
个人觉得replace不安全

update t1 set a=left(a,3) + '4 ' + right(a,len(a)-3)
------解决方案--------------------
update tb_1 set tb_name=stuff(tb_name,1,3, '1234 ')
or
update tb_1 set tb_name=replace(tb_name, '123 ', '1234 ')
------解决方案--------------------
declare @t table(id int identity ,s varchar(50))
insert into @t(s) select '123/234234 '
union all select '123/afsdf '
union all select '123/oi23# '
union all select '123/89324 '
union all select '123/@#$ '
union all select '123/2323 '
declare @aString varchar(100),@i int,@id int,@str varchar(100)

declare cus cursor for
select * from @t
open cus
fetch next from cus into @id,@aString
while @@fetch_status=0
begin
set @aString=rtrim(ltrim(@aString))
set @i=charindex( '/ ',@aString)
if(@i> 0)
begin
set @str=left(@aString,@i-1)
update @t set s=replace(@str, '123 ', '1234 ')+right(@aString,len(@aString)-@i+1) where [id]=@id
end
fetch next from cus into @id,@aString
end
deallocate cus

select * from @t