我必须在某个日期(样本日期)之前设置至少3天的提醒日期。如果该日期设置为周二或周三,则提醒日期将设置为周六和周日发送。我想把提醒日期提前4天设置为周二,周三提前5天。这样,我所有的提醒都会在一周内发送出去。我考虑过将dateadd与case语句一起使用,但不确定如何在datedd函数中合并某些工作日。
select dateadd (day,-3,convert(char(10),sample_date,101)) as Reminder_Sent
-- Ideally, my query should look like something like the below:
select
case when dateadd (monday,-3,convert(char(10),sample_date,101)) as Monday_Reminder_Sent
when dateadd (Tuesday,-4,convert(char(10),sample_date,101)) as Tuesday_Reminder_Sent
When dateadd (Wednesday,-5,convert(char(10),sample_date,101)) as Wednesday_Reminder_Sent
when dateadd (Thursday,-3,convert(char(10),sample_date,101)) as Thursday_Reminder_Sent
Else as Reminder_Sent
End发布于 2021-03-25 02:27:33
您可以使用datename进行比较。看看你的逻辑,你会有一个不同的计算,只有星期二和星期三,在这种情况下,它应该返回一个星期五,而不是周末。所有其他的日子你需要回到3天前。
declare @sample_date datetime
set @sample_date = getdate()
select @sample_date as date , case DATENAME ( weekday,@sample_date )
when 'Tuesday' then dateadd (day,-4,convert(char(10),@sample_date ,101))
when 'Wednesday' then dateadd (day,-5,convert(char(10),@sample_date ,101))
else dateadd (day,-3,convert(char(10),@sample_date,101))
end as Reminder_Sent 您也可以使用DATEPART。但您必须首先设置DATEFIRST。
https://stackoverflow.com/questions/66786706
复制相似问题