具有复杂 AND/OR 的案例陈述未按预期工作

发布于 2024-11-09 12:36:26 字数 1249 浏览 0 评论 0原文

我期望它返回“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷鸟归林 2024-11-16 12:36:26

将参数值替换为文字给出(对于第一种情况)

         WHEN ((('Saturday' = 'Saturday')  OR ('Saturday' = 'Sunday'))
                           AND 
               (( 'Saturday' <> 'Saturday') OR ( 'Saturday' <> 'Sunday')))

Which is

         WHEN ((True OR False) AND (False OR True))

Which is

         WHEN (True AND True)

Which is

         WHEN (True)

我认为你需要

SELECT
    CASE WHEN @WeekStartDay IN ('Saturday', 'Sunday') THEN 
        CASE WHEN @WeekEndDay IN ('Saturday', 'Sunday') THEN 
            'C'
        ELSE 
            'A'
        END 
    ELSE 
        CASE WHEN @WeekEndDay IN ('Saturday', 'Sunday') THEN 
            'B'
        ELSE 
            NULL /*You haven't said what should happen here?*/
        END 
    END
 AS Result

Substituting the parameter values in as literals gives (for the first case)

         WHEN ((('Saturday' = 'Saturday')  OR ('Saturday' = 'Sunday'))
                           AND 
               (( 'Saturday' <> 'Saturday') OR ( 'Saturday' <> 'Sunday')))

Which is

         WHEN ((True OR False) AND (False OR True))

Which is

         WHEN (True AND True)

Which is

         WHEN (True)

I think you need

SELECT
    CASE WHEN @WeekStartDay IN ('Saturday', 'Sunday') THEN 
        CASE WHEN @WeekEndDay IN ('Saturday', 'Sunday') THEN 
            'C'
        ELSE 
            'A'
        END 
    ELSE 
        CASE WHEN @WeekEndDay IN ('Saturday', 'Sunday') THEN 
            'B'
        ELSE 
            NULL /*You haven't said what should happen here?*/
        END 
    END
 AS Result
独夜无伴 2024-11-16 12:36:26

AND 表示两个条件都为真,OR 表示其中一个条件为真。

@WeekStartDay 是“星期六”。
@WeekEndDay 是“星期六”。

因此,如果您开始将其全部分解:

A: (TRUE OR FALSE) AND (FALSE OR TRUE) = TRUE AND TRUE = TRUE

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: (TRUE OR FALSE) AND (FALSE OR TRUE) = TRUE AND TRUE = TRUE

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.

尸血腥色 2024-11-16 12:36:26

(@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')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文