Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combine CASE statements

Author  Topic 

TansRYA
Starting Member

3 Posts

Posted - 2010-10-10 : 13:35:15
(CASE A
WHEN THEN
ELSE
END AS 'A1' + ' ' +
CASE B
WHEN THEN
ELSE
END AS 'B1') AS A1 + ' ' + B1

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-10-10 : 13:48:27
What is your question?
Go to Top of Page

TansRYA
Starting Member

3 Posts

Posted - 2010-10-10 : 14:17:12
that would help!!!...

How do i combine case statements. I want to be able to alias each column as well as combine them into another column.
Go to Top of Page

TansRYA
Starting Member

3 Posts

Posted - 2010-10-10 : 14:39:19
Example I have a table that has the following columns

[Column1]
[Column2]

the above columns are boolean, to start with i want to return 0=NULL and 1=Dog plus i want to alias each column i.e. Col1 and Col2

then i want to combine Col1 and Col2 and alias this as 'Animals' as well as returning Col1 and Col2 in the query.

I should be left with

Col1 Col2 Animals
Dog NULL Dog
NULL Cat Cat
Dog Cat Dog, Cat
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-10-10 : 22:50:50
ok, I think I understand. Does this work for you?

select Col1, Col2, coalesce(Col1, Col2) [Animals]
from ( select case Column1 when 0 then null when 1 then 'Dog' end [Col1],
case Column2 when 0 then null when 1 then 'Dog' end [Col2]
from @t
) d(Col1, Col2)
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2010-10-10 : 22:52:05
oh, I assume Col2 should be 'cat' ?
Go to Top of Page
   

- Advertisement -