具有复杂 AND/OR 的案例陈述未按预期工作
我期望它返回“C”的值,但它返回“A”。我似乎无法弄清楚为什么这不起作用,因为我的逻辑看起来很合理。
DECLARE @WeekStartDay varchar(10)
DECLARE @WeekEndDay varchar(10)
SET @WeekStartDay = 'Saturday'
SET @WeekEndDay = 'Saturday'
SELECT
CASE
WHEN (
((@WeekStartDay = 'Saturday') OR (@WeekStartDay = 'Sunday'))
AND
((@WeekEndDay <> 'Saturday') OR (@WeekEndDay <> 'Sunday'))
)
THEN 'A'
WHEN (
((@WeekEndDay = 'Saturday') OR (@WeekEndDay = 'Sunday'))
AND
((@WeekStartDay <> 'Saturday') OR (@WeekStartDay <> 'Sunday'))
)
THEN 'B'
WHEN (
((@WeekEndDay = 'Saturday') OR (@WeekEndDay = 'Sunday'))
AND
((@WeekStartDay = 'Saturday') OR (@WeekStartDay = 'Sunday'))
)
THEN 'C'
END AS Result
我的预期功能是:
我试图让它确定
IF @WeekStartDay = 星期六或星期日 AND @WeekEndDay 不是星期六或星期日 THEN 'A'
如果 @WeekEndDay = 星期六或星期日 AND @WeekStartDay 不是星期六或星期日 THEN 'B'
IF @WeekStartDay AND @WeekEndDay BOTH = Saturday or Sunday THEN 'C'
嗯,从回复来看,我的逻辑比我想象的要严重得多。
I am expecting this to return the value of 'C', but it is returning 'A'. I cannot seem to figure out why this will not work as my logic looks sound.
DECLARE @WeekStartDay varchar(10)
DECLARE @WeekEndDay varchar(10)
SET @WeekStartDay = 'Saturday'
SET @WeekEndDay = 'Saturday'
SELECT
CASE
WHEN (
((@WeekStartDay = 'Saturday') OR (@WeekStartDay = 'Sunday'))
AND
((@WeekEndDay <> 'Saturday') OR (@WeekEndDay <> 'Sunday'))
)
THEN 'A'
WHEN (
((@WeekEndDay = 'Saturday') OR (@WeekEndDay = 'Sunday'))
AND
((@WeekStartDay <> 'Saturday') OR (@WeekStartDay <> 'Sunday'))
)
THEN 'B'
WHEN (
((@WeekEndDay = 'Saturday') OR (@WeekEndDay = 'Sunday'))
AND
((@WeekStartDay = 'Saturday') OR (@WeekStartDay = 'Sunday'))
)
THEN 'C'
END AS Result
My intended function is:
I am trying to get it to deteremine
IF the @WeekStartDay = Saturday or Sunday AND @WeekEndDay IS NOT Saturday or Sunday THEN 'A'
IF the @WeekEndDay = Saturday or Sunday AND @WeekStartDay IS NOT Saturday or Sunday THEN 'B'
IF @WeekStartDay AND @WeekEndDay BOTH = Saturday or Sunday THEN 'C'
Hmmm from responses looks like my logic was way more off than I thought.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将参数值替换为文字给出(对于第一种情况)
Which is
Which is
Which is
我认为你需要
Substituting the parameter values in as literals gives (for the first case)
Which is
Which is
Which is
I think you need
AND 表示两个条件都为真,OR 表示其中一个条件为真。
@WeekStartDay
是“星期六”。@WeekEndDay
是“星期六”。因此,如果您开始将其全部分解:
A
获胜,因为它是满足您要求的第一个条件。您必须进一步解释您要做什么,因为我看不到需要做什么来帮助修复代码。
AND means when both conditions are true, OR means when either condition is true.
@WeekStartDay
is "Saturday".@WeekEndDay
is "Saturday".Therefore if you start breaking it all down:
A
wins because it is the first condition to match your requirements.You'll have to explain further what you are trying to do, because I cannot see what I'd need to do to help fix the code.
(@WeekEndDay <> '星期六') 或 (@WeekEndDay <> '星期日') 是问题所在。
这将永远返回 true。
有同样的问题
情况 B:与(@WeekStartDay <> '星期六') 或 (@WeekStartDay <> '星期日')
(@WeekEndDay <> 'Saturday') OR (@WeekEndDay <> 'Sunday') is the problem.
That will always return true.
Case B: has the same issue with
(@WeekStartDay <> 'Saturday') OR (@WeekStartDay <> 'Sunday')