我有一张基于我需要返回上一个工作日期的假期表。
Table_holiday
Id Date Text
1 2013-03-29 Good Friday
2 2013-05-01 Maharashtra day
3 2013-05-02 Holiday
当我执行date_recursive
功能时,我必须检查并返回last_business
日期
For example
如果我在@date datetime = '2013-03-29'
执行它,它应该返回last working date '2013-03-28'
,因为这是最后一个工作日期
sql中的新手的任何帮助。
答案 0 :(得分:3)
请尝试:
DECLARE @Table_holiday as TABLE(id int, [date] datetime, [Text] nvarchar(50))
insert into @Table_holiday values(1, '2013-03-29', 'Good Friday')
insert into @Table_holiday values(2, '2013-05-01', 'Maharashtra day')
insert into @Table_holiday values(3, '2013-05-02', 'Holiday')
declare @date datetime
set @date ='2013-03-29'
;with T(dt) as
(
select @date union all
select T1.[date] from T inner join @Table_holiday T1 on T1.[date]=T.dt-1
)select min(dt)-1 from T
答案 1 :(得分:3)
假设您的假期表中的任何日期不是工作日期,您可以向后搜索,直到找到非假期。
;with cte(adate) as (
select @date
from table_holiday
where @date = Date
union all
select h.Date
from cte
join table_holiday h on dateadd(d,-1,cte.adate) = h.Date
)
select isnull((select dateadd(d,-1,min(adate)) from cte), @date);
答案 2 :(得分:0)
select (case when DATEDIFF(day,GETDATE(),'2008-08-05')<2 Then "last
working date" Else "" END) from Table_holiday
希望它有所帮助。