2014-02-15 33 views

回答

1
DECLARE @mindate DATETIME 
DECLARE @maxdate DATETIME 
DECLARE @diff INT 
SET @maxdate = '2014-02-15' 
SET @mindate = '2014-01-15' 

SET @diff = DATEDIFF(DAY, @mindate,@maxdate) 
;WITH cte(dt,level) 
AS 
(
    SELECT @mindate AS dt, 0 AS level 
    UNION ALL 
    SELECT DATEADD(day,1,cte.dt),level + 1 from cte WHERE level < @diff 
) 
SELECT dt FROM cte 
option (maxrecursion 32767) 

選項MAXRECURSION需要如果開始日期和結束日期是超過100天的間隔

在SQLFiddle檢查查詢:http://www.sqlfiddle.com/#!3/fd09f/10

+0

謝謝dipendu保羅 –

2
Declare @start date='2014-01-15' 
Declare @Enddate date='2014-02-15' 
;with CTE as 
(select @start dates 
union all 
select DATEADD(day,1,dates) from cte where dates<@Enddate 

) 
select * from cte 
+0

它顯示下一個日期也 –

相關問題