如何使用SQL在接下来的5年中每个月选择同一天?
例如:
我试过了:
select dateadd(day,-day(dateadd(month,1,current_timestamp)) , dateadd(month,1,current_timestamp) )
怎么样
DECLARE @StartDate DATETIME = '05 Jan 2013', @YearsAdded INT = 5 ;WITH Dates AS ( SELECT @StartDate [Date] UNION ALL SELECT DATEADD(MONTH,1,[Date]) FROM Dates WHERE DATEADD(MONTH,1,[Date]) <= DATEADD(YEAR,@YearsAdded,@StartDate) ) SELECT * FROM Dates OPTION (MAXRECURSION 0)