我的问题与此类似 Get a list of dates between two dates using a function重复使用此代码10天:Declare @startDate datetimeDeclare @endDate datetimeset @startDate= '03/01/2019 12:00:00'set @endDate = '04/30/2019 12:00:00' ;WITH mycte AS( SELECT CAST(@startDate AS DATETIME) DateValue UNION ALL SELECT DateValue + 10 FROM mycte WHERE DateValue + 10 < @endDate - 1)SELECT DateValueFROM mycteOPTION (MAXRECURSION 0)我得到了ff的结果:2019-03-20 12:00:00.0002019-03-30 12:00:00.0002019-04-09 12:00:00.0002019-04-19 12:00:00.000但我希望结果是:2019-03-20 12:00:00.0002019-03-30 12:00:00.0002019-03-31 12:00:00.0002019-04-09 12:00:00.0002019-04-10 12:00:00.0002019-04-20 12:00:00.0002019-04-21 12:00:00.0002019-04-30 12:00:00.000SQL可能吗? 最佳答案 我发现问题相当神秘,但这似乎可以满足您的要求:set @startDate= '2019-03-10 12:00:00';set @endDate = '2019-04-30 12:00:00';WITH mycte AS ( SELECT CAST(@startDate AS DATETIME) as DateValue UNION ALL SELECT CONVERT(DATETIME, EOMONTH(DateValue)) + CONVERT(DATETIME, CONVERT(TIME, DateValue)) FROM mycte WHERE MONTH(DateValue + 10) <> MONTH(DateValue) AND CONVERT(DATE, DATEVALUE) <> CONVERT(DATE, EOMONTH(DateValue)) AND DateValue < @endDate UNION ALL SELECT DateValue + 10 FROM mycte WHERE DateValue + 10 < @endDate )SELECT DISTINCT DateValueFROM mycteORDER BY DateValueOPTION (MAXRECURSION 0); Here是db 小提琴。
09-25 17:49