CAST in AND statement

  • I am trying to use CAST in a case statement with two clauses but am stuck up with the syntax.

    Can anyone advise.

    CAST(Case when W.Employee_Type like '%CX5000%' and

    W.Employee_Policy AS VARCHAR(10)) = 'Yes' then 1 else 0 end as Count1

  • You have a problem with your nesting. You're opening your cast before the case and closing it in the middle of the case. It has to be either all out or all in.

    This might be closer to what you need.

    Case when CAST(W.Employee_Type AS VARCHAR(10)) like '%CX5000%' and

    CAST(W.Employee_Policy AS VARCHAR(10)) = 'Yes' then 1 else 0 end as Count1

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you. Got it working...

    SELECTCAST(

    CASE WHEN someCondition THEN someValue

    ELSE otherValue

    END

    as CHAR(11)

    )

    FROMtableExpression

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply