我需要在SQL Server中找到两个日期之间的日期。
where
startdate = '2015-12-04 00:00:00.000'
and enddate = '2015-12-07 00:00:00.000'
结果应为
2015-12-04 00:00:00.000
2015-12-05 00:00:00.000
2015-12-06 00:00:00.000
2015-12-07 00:00:00.000
答案 0 :(得分:0)
尝试:
DECLARE @Date1 DATE, @Date2 DATE
SET @Date1 = '20151204'
SET @Date2 = '20151210'
SELECT DATEADD(DAY,number+1,@Date1) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(DAY,number+1,@Date1) < @Date2
或者:
declare @startDate date;
declare @endDate date;
select @startDate = '20151204';
select @endDate = '20151210';
with interdate as
(
select dt = dateadd(dd, 1, @startDate)
where dateadd(dd, 1, @startDate) < @endDate
union all
select dateadd(dd, 1, dt)
from interdate
where dateadd(dd, 1, dt) < @endDate
)
select *
from interdate
答案 1 :(得分:0)
这将在sqlserver 2005中起作用:
DECLARE @startdate datetime
DECLARE @enddate datetime
SELECT @startdate ='2015-12-04', @enddate='2015-12-07'
;WITH N(N)AS
(SELECT 1 FROM(SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1)M(N)),
tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a,N b,N c,N d,N e,N f)
SELECT top (datediff(d, @startdate, @enddate) + 1)
dateadd(d, N - 1, @startdate)
FROM tally
结果(我删除了时间段):
2015-12-04
2015-12-05
2015-12-06
2015-12-07