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]
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]