这个调整数据的问题用SQL语句能解决吗?

cfqmxh 2005-05-18 11:06:50
我有一表:cw_warrant
warrantcode typeid warrantmonth
0001 1 01
0002 1 01
0003 1 01
0005 1 01
0006 1 01
0009 1 01
0010 1 01
0012 1 01
0015 1 01
我想将后面的号码向前移以填充前面空的号码,上面移动后为:
warrantcode typeid warrantmonth
0001 1 01
0002 1 01
0003 1 01
0004 1 01
0005 1 01
0006 1 01
0007 1 01
0008 1 01
0009 1 01
这有SQL语句能解决吗?
...全文
68 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziping 2005-05-18
  • 打赏
  • 举报
回复
update cw_warrant set warrantcode=cast(cast(warrantcode as int )-1 as varchar(4)) where
cast(warrantcode as int )-1 not in (select cast(warrantcode as int ) from cw_warrant )

update cw_warrant set warrantcode=replicate('0',4-len(warrantcode))+warrantcode where len(warrantcode)<4
子陌红尘 2005-05-18
  • 打赏
  • 举报
回复
create table cw_warrant(warrantcode varchar(10),typeid int,warrantmonth varchar(10))
insert into cw_warrant select '0001',1,'01'
insert into cw_warrant select '0002',1,'01'
insert into cw_warrant select '0003',1,'01'
insert into cw_warrant select '0005',1,'01'
insert into cw_warrant select '0006',1,'01'
insert into cw_warrant select '0009',1,'01'
insert into cw_warrant select '0010',1,'01'
insert into cw_warrant select '0012',1,'01'
insert into cw_warrant select '0015',1,'01'


while exists(select 1 from cw_warrant a where a.warrantcode != (select right('0000'+rtrim(cast(max(warrantcode) as int)+1),4) from cw_warrant where warrantcode<a.warrantcode))

update a
set
warrantcode = (select right('0000'+rtrim(cast(max(warrantcode) as int)+1),4) from cw_warrant where warrantcode<a.warrantcode)
from
cw_warrant a
where
a.warrantcode != (select right('0000'+rtrim(cast(max(warrantcode) as int)+1),4) from cw_warrant where warrantcode<a.warrantcode)


select * from cw_warrant
cfqmxh 2005-05-18
  • 打赏
  • 举报
回复
没人会吗?很急呀!
cfqmxh 2005-05-18
  • 打赏
  • 举报
回复
非常感激!极端佩服!谢谢!结贴.

34,575

社区成员

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

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