关于更新表中字段部分内容的问题

jamk 2011-08-03 04:09:34
一个表TB内容如下:
mem_card_no
1009000001
1009000002
1009000003
1009000004
1009000005
1009000006
1009000007
1009000008
1009000009
1009000010
1009000011
1009000012
1009000013
1009000014
1009000015
1009000016
1009000017
1009000018
1009000019
1009000020
1009000021
1009000022
1009000023
1009000024
1009000025
1009000026
1009000027
1009000028
1009000029
1009000030
1009000031
1009000032
1009000033
1009000034
.
.
.
现在想把他更新为:
2011000001
2011000002
2011000003
2011000004
2011000005
2011000006
2011000007
2011000008
.
.
.

求正解,简单要求就是将其中1009部分更新为2011,谢谢
...全文
147 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
勿勿 2011-08-04
  • 打赏
  • 举报
回复
update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
oO寒枫Oo 2011-08-04
  • 打赏
  • 举报
回复

update TB
set mem_card_no='2011'+right(mem_card_no,len(mem_card_no)-4)
where left(mem_card_no,4)='1009'

楼主 你自己的的有点问题 慎用
wolf_ben 2011-08-04
  • 打赏
  • 举报
回复
建议还是用小三的为好,repalce没限制条件的话很容易更新错误,使数据出错,这会很麻烦的。。。
Alawn_Xu 2011-08-04
  • 打赏
  • 举报
回复
update TB set mem_card_no=replace(mem_card_no,'1009','2011')

cutebear2008 2011-08-04
  • 打赏
  • 举报
回复
顶之!
[Quote=引用 14 楼 chtzhking 的回复:]
引用 3 楼 acherat 的回复:

SQL code

update tb
set col = '2011' + right(col,len(col)-4)
where left(col,4) = '1009'

恭喜,建议用3楼的方法
[/Quote]
cutebear2008 2011-08-04
  • 打赏
  • 举报
回复

update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
chtzhking 2011-08-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acherat 的回复:]

SQL code

update tb
set col = '2011' + right(col,len(col)-4)
where left(col,4) = '1009'
[/Quote]
恭喜,建议用3楼的方法
cutebear2008 2011-08-04
  • 打赏
  • 举报
回复



CREATE TABLE TB (
mem_card_no VARCHAR(20)
)

INSERT INTO TB
SELECT '1009000001'
UNION ALL
SELECT '1009000002'
UNION ALL
SELECT '1009000003'

SELECT * FROM tb

update tb set mem_card_no=replace(mem_card_no,'1009','2011')

SELECT * FROM tb

mem_card_no
--------------------
2011000001
2011000002
2011000003

(3 row(s) affected)
gogodiy 2011-08-04
  • 打赏
  • 举报
回复
恭喜楼主解决问题,另外几位大大说的对,应该注意条件的限制。
PS:那就赶紧接分吧~~
jamk 2011-08-04
  • 打赏
  • 举报
回复
嗯嗯 谢谢各位大大~ 结贴
chuanzhang5687 2011-08-03
  • 打赏
  • 举报
回复
求解分
--小F-- 2011-08-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kongmajian 的回复:]
SQL code
update TB set mem_card_no=replace(mem_card_no,'1009','2011')


...刚发完就找到答案了。。
[/Quote]
gx
叶子 2011-08-03
  • 打赏
  • 举报
回复

declare @T table (mem_card_no varchar(16))
insert into @T
select 1009000001 union all
select 1009000002 union all
select 1009000003 union all
select 1009000004 union all
select 1009000005 union all
select 1009000006 union all
select 1009000007 union all
select 1009000008 union all
select 1009000009 union all
select 1009000010 union all
select 1010000011 union all
select 1010000012 union all
select 1010000013 union all
select 1010000014 union all
select 1010000015

select * from @T

update @T set mem_card_no= case when left(mem_card_no,4)=1009
then '2011'+right(mem_card_no,len(mem_card_no)-4) else mem_card_no end

select * from @T
/*
mem_card_no
----------------
2011000001
2011000002
2011000003
2011000004
2011000005
2011000006
2011000007
2011000008
2011000009
2011000010
1010000011
1010000012
1010000013
1010000014
1010000015
*/
--也可以用where 局部更新
update tb set col = '2011' + right(col,len(col)-4)
where left(col,4) = '1009'
叶子 2011-08-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kongmajian 的回复:]

SQL code
update TB set mem_card_no=replace(mem_card_no,'1009','2011')


...刚发完就找到答案了。。
[/Quote]
replace容易出问题,建议还是用

update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
AcHerat 元老 2011-08-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kongmajian 的回复:]

SQL code
update TB set mem_card_no=replace(mem_card_no,'1009','2011')


...刚发完就找到答案了。。
[/Quote]

建议还是限定下更新条件,楼主这个会把里面的 1009 也会更新为 2011

如果有 1009001009 更新完会是 2011002011 。。。
快溜 2011-08-03
  • 打赏
  • 举报
回复
replace可能导致数据错误
快溜 2011-08-03
  • 打赏
  • 举报
回复
update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
AcHerat 元老 2011-08-03
  • 打赏
  • 举报
回复

update tb
set col = '2011' + right(col,len(col)-4)
where left(col,4) = '1009'
一缕青烟 2011-08-03
  • 打赏
  • 举报
回复


update 表 set 字段=REPLACE(字段,'1009','2011')
jamk 2011-08-03
  • 打赏
  • 举报
回复
update TB set mem_card_no=replace(mem_card_no,'1009','2011') 

...刚发完就找到答案了。。

34,588

社区成员

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

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