Conditional or ternary operators...?

  • Hi,

    This is what I am trying to accompolish in a select statement. Is there an equivalent of a ternary operator in sql? Can any one tell me how to do this, preferably in a single query?

    SELECT column1, (column2 == "Y"? abc : xyz) FROM table1

    Thanks.

  • use case statement like this

    select column1,case column2 when 'Y' then 'abc' else 'xyz' end column2 from table1

  • Thanks. Didn't know t-sql has case statements.

  • Not to nit-pick but CASE is a function in SQL, not a statement.  All of the THEN and ELSE clauses must evaluate to single value, and all of the values must be of a compatible datatype.  The first true WHEN clause determines which value is the return value of the function.

    Don't confuse it with SELECT CASE or SWITCH in other languages, you can't include arbitrary blocks of code in the THEN clauses.

  • Good Catch..

    Yes, Case is a function in T-SQL

     

Viewing 5 posts - 1 through 4 (of 4 total)

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