sql 取得某個時間段內的全部月份和日期

Sql 取得某個時間段內的全部月份sql

1 declare @begin datetime,@end datetime
2 set @begin='2015-2-6'
3 set @end='2015-12-2'
4  
5 declare @months int
6 set @months=DATEDIFF(month,@begin,@end)
7 select convert(varchar(7),DATEADD(month,number,@begin) ,120) AS 月份
8 from master.dbo.spt_values
9 where type='p' AND number<=@months

效果圖spa

sql 某個時間段內的全部日期 code

1 DECLARE @days INT, 
2 @date_start DATETIME = '2017-03-15', 
3 @date_end DATETIME = '2017-04-13'  
4 SET @days = DATEDIFF(DAY, @DATE_START, @DATE_END);   
5  
6     SELECT convert(char(10), DATEADD(dd, number, @DATE_START),120) AS yyyymmdd 
7     FROM    master.dbo.spt_values  as spt
8     WHERE   type = 'p' 
9     AND number <= @days

效果圖blog

相關文章
相關標籤/搜索