我有以下查询
select Case.casekey, LoginName, startdatedate, SenttoClientDate
from case with (nolock)
where SenttoClientDate = dateadd(day,-1, cast(getdate() as date))
这将返回{senttoclientdate} =昨天的结果。
问题是,如果我在星期一运行这个,我会得到星期天的结果,但我想要上一个工作日。因此,如果我在星期一跑,我想要星期五的结果。
答案 0 :(得分:1)
你可以这样做:
WHERE SenttoClientDate = (SELECT DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE())
WHEN 'Sunday' THEN -2
WHEN 'Monday' THEN -3
ELSE -1 END, DATEDIFF(DAY, 0, GETDATE())))
答案 1 :(得分:0)
在日期部分中加入案例
where SentToClientDate =
case when datepart(dw,getdate()) = 2 then dateadd(day,-3,getdate()) -- Monday is day 2, for no reason whatsoever
else dateadd(day,-1,getdate())
end
答案 2 :(得分:0)
在何处条件下使用套管如下,
select Case.casekey, LoginName, startdatedate, SenttoClientDate
from case with (nolock)
where SentToClientDate =
case when datepart(dw,getdate()) = 2 then dateadd(day,-3,getdate())
when datepart(dw,getdate()) = 1 then dateadd(day,-2,getdate())
else dateadd(day,-1,getdate())
end
答案 3 :(得分:0)
请你试试这个,这是非常简单和完美的解决方案:
SELECT Case.casekey, LoginName, startdatedate, SenttoClientDate
FROM case with (nolock)
WHERE SenttoClientDate =
CAST(DATEADD(dd,
CASE WHEN DATEPART(weekday, GETDATE()) = 6 THEN 0
ELSE (6-(DATEPART(weekday, GETDATE())+7)) END, GETDATE()) AS DATE)