SQL根据两个日期生成年、月、日
1 DECLARE @beginTime DATETIME, @endTime DATETIME 2 SET @beginTime ='2019-03-01' 3 SET @endTime ='2019-12-31 23:59:59' 4 5 --1.生成年份 6 SELECT CONVERT(VARCHAR(4),DATEADD(YEAR,number,@beginTime),120) AS 年 7 FROM master..spt_values 8 WHERE type='p' AND DATEADD(YEAR,number,@beginTime)<=@endTime 9 10 --2.生成月份 11 SELECT CONVERT(VARCHAR(7),DATEADD(MONTH,number,@beginTime),120) AS 月份 12 FROM master..spt_values 13 WHERE type='p' AND DATEADD(MONTH,number,@beginTime)<=@endTime 14 15 --3.生成日期 16 SELECT CONVERT(VARCHAR(10),DATEADD(DAY,number,@beginTime),120) AS 日期 17 FROM master..spt_values 18 WHERE type='p' AND DATEADD(DAY,number,@beginTime)<=@endTime