tags:

views:

48

answers:

5

So this isn't really a normal conditional select statement. I'm helping a colleague out with some SQL and we have this issue.

I current there is a coulumn ID and a column Number. Either ID is 0 or Number is 0.

So my select statement needs to do Select colA, colB, colC, crazy part From Table A.

the crazy part is this: (If ID > 0, ID, Number) basically select and return that column that isn't 0. Is there any way to do this in T-SQL?

+2  A: 

Use the CASE statement:

http://msdn.microsoft.com/en-us/library/ms181765.aspx

Ian Jacobs
+10  A: 

This should do it:

select colA, colB, colC, case when ID is 0 then Number else ID end as Crazy
from TableA
roufamatic
/facepalm I tried everything but a case statement, thanks!
msarchet
A: 

What you are looking for is CASE

keithwarren7
A: 

You would do this using a CASE statement:

SELECT
    colA,
    colB,
    colC,
    CASE WHEN ID > 0 THEN ID ELSE Number END
FROM
   TableA
Matt Weldon
+3  A: 

CASE (as explained by others) certainly works. However, if one of the two is always 0, you could also just add them together.

Lucero