------一个有意思的题目-----------

本文介绍如何使用SQLServer2000将不连续的整数序列转换为带有范围标识的字符串形式,例如将一系列独立的数字转换为类似'1-5, 12, 17-20, 25'的格式。

请教各位:
数据记录是这样的
1
2
3
4
5
12
17
18
19
20
25
请问sqlserver2000如何显示成这样1-5,12,17-20,25

 

declare @t table(num int)
insert into @t select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 12
union all select 17
union all select 18
union all select 19
union all select 20
union all select 25

select
   
rtrim(a.num)+(case when min(b.num)!=a.num then '-'+rtrim(min(b.num)) else '' end)
from
    (
select t.num from @t t where not exists(select 1 from @t where num=t.num-1)) a,
    (
select t.num from @t t where not exists(select 1 from @t where num=t.num+1)) b
where
    a.num
<=b.num
group by a.num /*
-------------------------
1-5
12
17-20
25
*/
 

 

 

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值