DECLARE @dbegin DATE = DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0); --获取本年第一天 DECLARE @dend DATE = DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0)); --本年最后一天 WITH ctea (ddate) AS (SELECT @dbegin AS ddate UNION ALL SELECT DATEADD(DAY, 1, ddate) FROM ctea WHERE ddate < @dend) SELECT MONTH(ddate) AS imonth, COUNT(*) AS idays FROM ctea GROUP BY MONTH(ddate) OPTION (MAXRECURSION 0);
如何计算本年度的每个月有多少天?