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

效果图

sql 取得某个时间段内的所有月份和日期

sql 某个时间段内的所有日期 

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

效果图

sql 取得某个时间段内的所有月份和日期

 

相关文章:

  • 2022-12-23
  • 2021-07-12
  • 2021-12-01
  • 2022-12-23
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2022-12-23
  • 2021-12-03
  • 2022-02-01
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
相关资源
相似解决方案