2

What is wrong with my case statement below?

If the value is numeric then I want to check if its greater than or equal to 5 and return 1 if true else return 0. Is there any elegant way of doing this?

Below CASE is nested in another CASE

CASE
    WHEN(
        xyz <> a
        AND abc <> 3
        AND
            CASE 
                WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 THEN 
                    CASE 
                        WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 THEN 1
                        ELSE 0
                    END
                ELSE 0
            END
    )
    THEN 1
    ELSE 0

I get the below error on the first CASE after the second AND before WHEN ISNUMERIC()

An expression of non-boolean type specified in a context where a condition is expected.

1 Answer 1

5

Missing criteria in your outermost case statement:

CASE
    WHEN(
        xyz <> a
        AND abc <> 3
        AND
            CASE 
                WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 THEN 
                    CASE 
                        WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 THEN 1
                        ELSE 0
                    END
                ELSE 0
            END = ?  -- Missing criteria
    )  
    THEN 1
    ELSE 0

Edit: Not sure there's an ideal way to format this, but I find the following to be easier to follow:

CASE WHEN (    xyz <> a      
           AND abc <> 3  
           AND CASE WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 
                    THEN CASE WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 
                              THEN 1
                              ELSE 0
                         END
                    ELSE 0
               END = ? -- Missing Criteria
           ) 
     THEN 1
     ELSE 0
END
Sign up to request clarification or add additional context in comments.

5 Comments

outermost case statement It's not the outer most. It's sorta in the middle
@ConradFrix I see it as the WHEN of the outermost case statement is missing a comparison.
What should the last END equal to? 0 or 1?
@user793468 if you want to be true when RepCode > 5 then 1
@GoatCO In the edited version, you have '= ?' after the parenthesis. I believe it should be inside the parenthesis after the END.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.