我想获取两个日期之间缺少的日期
说 @maxDate = '2013-01-28'
说 @curDate = GetDate()
我已经写了 CTE 来做到这一点。这是我的 CTE:
create procedure EmpDate
as begin
declare @curDate Date
set @curDate = GETDATE()
declare @maxDate Date
select @maxDate = MAX(EmpAttendance.Sdate)
from EmpAttendance
;with GetDates As
(
select 1 as counter, @maxDate as Date
UNION ALL
select counter + 1, DATEADD(day,counter,@maxDate)
from GetDates
where DATEADD(day, counter, @maxDate) < @curDate
)
select Date from GetDates
end
go
这样做的结果是
Date
2013-01-28
2013-01-29
2013-01-30
但我想要
2013-01-29
2013-01-30
请帮帮我。
最佳答案
改变
select 1 as counter, @maxDate as Date
到
select 1 as counter, DATEADD(day,1,@maxDate) as Date
通过更改 CTE 使其更简单
;with GetDates As
(
select DATEADD(day,1,@maxDate) as TheDate
UNION ALL
select DATEADD(day,1, TheDate) from GetDates
where TheDate < @curDate
)
...
关于sql-server-2008 - CTE 使用 SQL Server 获取两个日期之间的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14626473/