我有這個表:插入一系列日期到SQL開始日期和結束日期輸入
**ID StartDate EndDate**
1 01/01/2012 03/01/2012
2 28/09/2013 02/10/2013
3 12/06/2011 15/06/2011
,我需要有這個表: 日期
**ID Date**
1 01/01/2012
1 02/01/2012
1 03/01/2012
2 28/09/2013
2 29/09/2013
2 30/09/2013
2 01/10/2013
2 02/10/2013
3 12/06/2011
3 13/06/2011
3 14/06/2011
3 15/06/2011
我旁邊SQL代碼Retarn起始日期&之間的日期的結束日期+起始日期+結束日期:
declare @Start datetime
declare @end datetime
declare @request int
set @Start = '2014-09-28 06:53:04.560'
set @end = '2014-09-29 11:53:04.560'
set @request = 1
;with Dates as (
select @request as reqId,@Start as reqDate
union all
select reqId+1,DATEADD(hh,1,reqDate) from Dates
where reqDate < @end
)
select * from Dates
如何獲得此批結果的StartDate-EndDate輸入結果?
使用日曆表可以讓您的查詢更加簡單。 http://blog.jontav.com/post/9380766884/calendar-tables-are-incredibly-useful-in-sql。 – mhn 2014-09-29 08:20:08