SQL Server Performance

Derive multiple columns from single "SELECT CASE" statement

Discussion in 'SQL Server 2005 T-SQL Performance Tuning' started by atulgoswami, Mar 26, 2010.

  1. atulgoswami New Member

    I am just thinking if it is possible to derive multiple columns from single "SELECT CASE" statement. I mean, based one condition, can i add two columns in select statement using single case.
    Thanks in advance
  2. Adriaan New Member

    No. CASE can only be used within a single expression, so you cannot make it control the output of separate columns.
    If it's a general condition for outputting different columns, then use control-of-flow syntax, using IF, to get to the right query.
    If the condition is based on specific values on rows, then you can either use UNION, or do a separate CASE statement for each output column.
    Funny that most people try to implement IF logic within a query, only to find out that they have to use CASE, and you're coming at it from the opposite direction.
  3. FrankKalis Moderator

    What you could do is to evaluate the expression once in a derived table, and then in the outer SELECT refer to this evaluation more than once.
    Can you describe what you want to achieve?

Share This Page