繁体   English   中英

如何在不创建新数据库对象的情况下生成日期序列?

[英]How to generate a serie of date without create a new database object?

我需要创建一个查询,该查询返回从2013年4月到现在的年份和月份,如下所示:

2013  | 4
2013  | 5
2013  | 6
2013  | 7
2013  | 8
....  | .
....  | .
....  | .
2015  | 1
2015  | 2
2015  | 3
2015  | 4

它必须与SQLServer 2000兼容,并且我在SE上找到的所有解决方案都仅与SQLServer2005 +兼容。

有没有办法解决这个(非常)旧的SGBD?

如果没有解决方案,我会考虑创建一个数字表,但我觉得它不是很优雅。

declare @Startdate AS DateTime = Dateadd( year,-2,Getdate()) , 
        @EndDate DateTime = Getdate()
Declare @YearTable as Table ( CalYear int , calmonth int)

While @Startdate <= @EndDate 
BEGIN 
    Insert Into @YearTable values(YEAR(@Startdate),MONTH(@Startdate))
    set @Startdate = DateADD(Month,1,@Startdate)
END 

select * from @YearTable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM