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
 concatinate value into new column upon select

Author  Topic 

silentthread
Starting Member

2 Posts

Posted - 2008-01-24 : 16:03:50
Hi,

I'm new to the forum, and semi new to sql, I've been working with
it for about 1 year now, but from a programmers side. I simply
just plug in my selects, and inserts, but most of my work is in
asp.net. I apologize if I have posted in the wrong forum section.
Here is my problem....

I have a table that has the follwing columns..
UserName, question1, question2, Id

question1 and question2 columns simply hold either 'Y' or 'N'
Meaning that the user either answered Y or N for the questions.

I'm trying to create another column on the fly... example....

select *,
[othercolumn] = case question1
when 'Y' then 'question1,'
else ''
end
from mytable

If both questions1 and questions2 have the column value of 'Y', then
the othercolumn should have 'question1,question2' as the end of the above select.

The above example only works for checking for question1, but I will need
to do the same for question2 and concatinate that into the othercolumn column.

I hope, I have explained myself well.
Please let me know if you need more info from me.

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-24 : 16:23:29
[code]SELECT
CASE WHEN question1 = 'Y'
THEN CASE WHEN question2 = 'Y'
THEN 'question1, question2'
ELSE 'question1'
END
WHEN question2 = 'Y'
THEN 'question2'
ELSE ''
END[/code]
Go to Top of Page

raaj
Posting Yak Master

129 Posts

Posted - 2008-01-24 : 16:33:35
i am not sure whether this is correct or not...just trying....

select *, othercolumn = case when question1 ='Y' and question2 = 'Y' then question1+','+question2
end
ur output will be something like this under othercolumn Y,Y

if u want ur output to be question1,question...then u can try this...
select *, othercolumn = case when question1 ='Y' and question2 = 'Y' then 'question1,question2'
end

is tht the one which ur looking for???
Go to Top of Page

silentthread
Starting Member

2 Posts

Posted - 2008-01-25 : 13:23:22
Thanks, this is awesome. I did not know about nesting SQL.
Go to Top of Page
   

- Advertisement -