0
土曜日または日曜日の場合、最後の作業日として金曜日を使用する必要がある場合は、前の5営業日を解決しようとしています。SQL作業日関数
Iveはこのクエリの他の部分を処理していますが、これを使用してエラーメッセージを受け取る関数を作成しようとすると、iveが逃したアイデアは何ですか?
create table holidays (
date date);
GO
create function dbo.WorkDays
(
@date datetime,
@days int
)
returns date
as
Begin
IF datename(dw,@date) = 'Saturday'
select dateadd(dd,-1,thedate)
from (
select thedate=dateadd(d,-v.day,cast(@date as date)),
rn=row_number() over (order by v.day),
weekday = datename(dw,dateadd(d,-v.day,cast(@date as date)))
from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day)
left join holidays h on h.date = dateadd(d,v.day,cast(@date as date))
where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S'
) x
where @days = rn
Else If datename(dw,@date) = 'Sunday'
select dateadd(dd,-1,thedate)
from (
select thedate=dateadd(d,-v.day,cast(@date as date)),
rn=row_number() over (order by v.day),
weekday = datename(dw,dateadd(d,-v.day,cast(@date as date)))
from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day)
left join holidays h on h.date = dateadd(d,v.day,cast(@date as date))
where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S'
) x
where @days = rn
Else
select thedate
from (
select thedate=dateadd(d,-v.day,cast(@date as date)),
rn=row_number() over (order by v.day),
weekday = datename(dw,dateadd(d,-v.day,cast(@date as date)))
from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day)
left join holidays h on h.date = dateadd(d,v.day,cast(@date as date))
where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S'
) x
where @days = rn
End
drop table holidays
エラーメッセージの内容を教えてください。 – dfundako