如何改动数据记录某个字段的值和根据某个字段更新另一个字段的值?

to_pty 2004-12-23 08:50:03
如表:
--------------------------------------
id biaoti niandu
--------------------------------------
C-1955-1 xxxxxxxxx ????
C-1955-2 xxxxxxxxx ????
C-1955-3 xxxxxxxxx ????
C-1956-1 xxxxxxxxx ????
C-1956-2 xxxxxxxxx ????
......
--------------------------------------
我想实现两个目的:
1、将id字段的值改为如:C1955-1,即将C和年度中间的-去掉。
2、将年度????改成根据id的年度现实。

更改后的表为:
--------------------------------------
id biaoti niandu
--------------------------------------
C1955-1 xxxxxxxxx 1955
C1955-2 xxxxxxxxx 1955
C1955-3 xxxxxxxxx 1955
C1956-1 xxxxxxxxx 1955
C1956-2 xxxxxxxxx 1955
......
--------------------------------------

工作急需,请各位高手指教,谢谢。
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuzhouhome 2004-12-23
  • 打赏
  • 举报
回复
update 表 set id=left(id,1) & right(id,6),niandu=substring(id,3,4)
joincsdn 2004-12-23
  • 打赏
  • 举报
回复
楼上说得已经够全了!!
Frewin 2004-12-23
  • 打赏
  • 举报
回复
全过程:
update 表 Set niandu = right(id,len(id)-Charindex('-',id,0))
update 表 Set niandu = left(niandu,Charindex('-',niandu,0)-1)


Update 表 Set id = Replace(id,'C-','C')
Frewin 2004-12-23
  • 打赏
  • 举报
回复
1 Update 表 Set id = Replace(id,'C-','C')
to_pty 2004-12-23
  • 打赏
  • 举报
回复
搞错了,更改后的表:
更改后的表为:
--------------------------------------
id biaoti niandu
--------------------------------------
C1955-1 xxxxxxxxx 1955
C1955-2 xxxxxxxxx 1955
C1955-3 xxxxxxxxx 1955
C1956-1 xxxxxxxxx 1956
C1956-2 xxxxxxxxx 1956
......
--------------------------------------

34,593

社区成员

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

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