August 21, 2006 at 3:13 pm
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.
August 21, 2006 at 3:20 pm
use case statement like this
select column1,case column2 when 'Y' then 'abc' else 'xyz' end column2 from table1
August 21, 2006 at 3:43 pm
Thanks. Didn't know t-sql has case statements.
August 29, 2006 at 1:01 pm
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.
August 29, 2006 at 1:33 pm
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