求一条SQL语句或者写法,字段中一部分数字重新编号问题

jackyqk 2008-01-09 04:47:14
通过一条SQL语句我找到一下结果
finlno
京咨[2008]第17984号
京咨[2008]第18075号
京咨[2008]第18129号
京咨[2008]第18130号
京咨[2008]第18131号...

现在需要把他恩改成
京咨[2008]第1号
京咨[2008]第2号
京咨[2008]第3号
京咨[2008]第4号
京咨[2008]第5号...
...全文
118 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
newqq 2008-01-09
  • 打赏
  • 举报
回复
高手如云啊
wzy_love_sly 2008-01-09
  • 打赏
  • 举报
回复
declare @tb table (col varchar(60))
insert @tb select '京咨[2008]第17984号'
insert @tb select '京咨[2008]第18075号'
insert @tb select '京咨[2008]第18129号'
insert @tb select '京咨[2008]第18130号'
insert @tb select '京咨[2008]第18131号'

select left(col,charindex('第',col))+cast(ROW_NUMBER() OVER (order by col) as varchar(10))+'号'
from @tb

select id=identity(int,1,1),* into # from @tb

select left(col,charindex('第',col))+cast(id as varchar(10))+'号' from #


京咨[2008]第1号
京咨[2008]第2号
京咨[2008]第3号
京咨[2008]第4号
京咨[2008]第5号
dobear_0922 2008-01-09
  • 打赏
  • 举报
回复
create table tb(col varchar(60))
insert tb select '京咨[2008]第17984号'
insert tb select '京咨[2008]第18075号'
insert tb select '京咨[2008]第18129号'
insert tb select '京咨[2008]第18130号'
insert tb select '京咨[2008]第18131号'


update tb
set col=left(col,charindex('第',col))+rtrim((select count(1) from tb t where col<=tb.col))+'号'

select * from tb
/*
col
------------------------------------------------------------
京咨[2008]第1号
京咨[2008]第2号
京咨[2008]第3号
京咨[2008]第4号
京咨[2008]第5号

(5 row(s) affected)
*/

drop table tb
jackyqk 2008-01-09
  • 打赏
  • 举报
回复
谢谢各位大大,我试试先
kk19840210 2008-01-09
  • 打赏
  • 举报
回复

create table tb(col varchar(60))
insert tb select '京咨[2008]第17984号'
insert tb select '京咨[2008]第18075号'
insert tb select '京咨[2008]第18129号'
insert tb select '京咨[2008]第18130号'
insert tb select '京咨[2008]第18131号'


update tb set col='京咨[2008]第'+convert(varchar(10),id)+'号' from
(
select col,id=(select count(1) from tb where col<=a.col) from tb a
)b where tb.col=b.col
chuifengde 2008-01-09
  • 打赏
  • 举报
回复
select finlno=replace(finlno,value,ltrim(cou))  
from
(select
finlno,
value=replace(right(finlno,len(finlno)-charindex('第',finlno)),'号',''),
cou=(select count(1) from [Table] where finlno<=a.finlno) from [Table] a
)aa
liangCK 2008-01-09
  • 打赏
  • 举报
回复
create table tb(col varchar(60))
insert tb select '京咨[2008]第17984号'
insert tb select '京咨[2008]第18075号'
insert tb select '京咨[2008]第18129号'
insert tb select '京咨[2008]第18130号'
insert tb select '京咨[2008]第18131号'

select left(col,charindex('第',col))+cast((select count(1) from tb where col<a.col)+1 as varchar)+right(col,1)
from tb a

drop table tb

/*

--------------------------------------------------------------------------------------------
京咨[2008]第1号
京咨[2008]第2号
京咨[2008]第3号
京咨[2008]第4号
京咨[2008]第5号

(所影响的行数为 5 行)
*/
yiyi_wx 2008-01-09
  • 打赏
  • 举报
回复
学习~
子陌红尘 2008-01-09
  • 打赏
  • 举报
回复
declare @t table(memo varchar(20))
insert into @t values('京咨[2008]第17984号')
insert into @t values('京咨[2008]第18075号')
insert into @t values('京咨[2008]第18129号')
insert into @t values('京咨[2008]第18130号')
insert into @t values('京咨[2008]第18131号')


update t
set
memo=left(memo,charindex('第',memo))+rtrim((select count(*) from @t where memo<=t.memo))+'号'
from
@t t

select * from @t

/*
memo
--------------------
京咨[2008]第1号
京咨[2008]第2号
京咨[2008]第3号
京咨[2008]第4号
京咨[2008]第5号
*/
liangCK 2008-01-09
  • 打赏
  • 举报
回复
create table tb(col varchar(60))
insert tb select '京咨[2008]第17984号'
insert tb select '京咨[2008]第18075号'
insert tb select '京咨[2008]第18129号'
insert tb select '京咨[2008]第18130号'
insert tb select '京咨[2008]第18131号'

declare @c int
set @c=0
update tb set @c=@c+1,
col=left(col,charindex('第',col))+cast(@c as varchar)+right(col,1)

select * from tb

drop table tb

/*
col
------------------------------------------------------------
京咨[2008]第1号
京咨[2008]第2号
京咨[2008]第3号
京咨[2008]第4号
京咨[2008]第5号

(所影响的行数为 5 行)
*/

34,590

社区成员

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

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