数据库日期年月期间段

sql server 数据库,日期期间段查询。
a.id+b.id*10+c.id*100+d.id*1000 表示从0到9999一共一万的连续数。
convert(varchar(10),dateadd(day,id,'2003-03-06'),120) 是获取到从指定日期开始连续的10000天日期,然后根据需要获取时间段获取日期。再把日期转化为字符串,用left()函数取得年和月,用distinct过滤重复数据,也就得到了,期间段年月

[color=red]select distinct left(convert(varchar(10),dateadd(day,id,'2003-03-06'),120),7) AS [DATE] from (
select id=a.id+b.id*10+c.id*100+d.id*1000 from
(
select id=0 union all select id=1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) a,(
select id=0 union all select id=1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) b,(
select id=0 union all select id=1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) c,(
select id=0 union all select id=1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) d
) aa
where dateadd(day,id,'2003-03-06')<'2004-05-01'
ORDER BY [DATE][/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值