tags:

views:

84

answers:

2

Hello, I have a huge query which uses case/when often. Now I have this SQL here, which does not work.

 (select case when xyz.something = 1
 then
     'SOMETEXT'
 else
      (select case when xyz.somethingelse = 1)
      then
          'SOMEOTHERTEXT'
      end) 

      (select case when xyz.somethingelseagain = 2)
      then
          'SOMEOTHERTEXTGOESHERE'
      end)
 end) [ColumnName],

Whats causing trouble is xyz.somethingelseagain = 2, it says it could not bind that expression. xyz is some alias for a table which is joined further down in the query. Whats wrong here? Removing one of the 2 case/whens corrects that, but I need both of them, propably even more cases.

Thanks :-)

+2  A: 

How about this

SELECT
   CASE 
   WHEN xyz.something = 1 THEN 'SOMETEXT'
   WHEN xyz.somethingelse = 1 THEN 'SOMEOTHERTEXT'
   WHEN xyz.somethingelseagain = 2 THEN 'SOMEOTHERTEXTGOESHERE'
   ELSE 'SOMETHING UNKNOWN'
   END AS ColumnName

Greets Flo

Florian Reischl
Works, whats the difference? :)
grady
Lets say I have some of the whens as duplicates, like this:WHEN xyz.something = 1 and xyz.abc = 2 THEN 'SOMETEXT'WHEN xyz.something = 1 and xyz.abc <> 2 THEN 'SOMEOTHERTEXT'Can I somehow say that if the first when was set that the upcoming ones are skipped? Something which escapes the case/when?
grady
+1  A: 

Lets say I have some of the whens as duplicates, like this:

WHEN xyz.something = 1 and xyz.abc = 2 THEN 'SOMETEXT'
WHEN xyz.something = 1 and xyz.abc <> 2 THEN 'SOMEOTHERTEXT'

Can I somehow say that if the first when was set that the upcoming ones are skipped? Something which escapes the case/when?

grady