I have a table sql server 2008 table called MyTestTable1
Here is my table and script:
So my table looks like this:
I need to write a query that does the following:
Look at columns MyCol1, MyCol2, MyCol3 and whichever of those columns does not equal to 0, use the value for the column from the detail column.
So in this case the column called MyCol3 doesn't not equal to zero. So then MyCol3 is the column with a value , so now instead of showing 6 I want to display the value in the detail column.
So my result set should be this:
So for example if MyCol2 had been the one with the value of 6 then my output result would have been this:
I started to write the query like this:
When ([MyCol1] <> 0) THEN [MyCol1Detail]
When ([MyCol2] <> 0) THEN [MyCol2Detail]
When ([MyCol3] <> 0) THEN [MyCol3Detail]
But I'm not sure I have the syntax correct or how to say name the resulting column whatever the name of the column with a value is?
can anyone help?