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
 Multiple Columns from CASE Expression?

Author  Topic 

Windza
Yak Posting Veteran

61 Posts

Posted - 2008-12-03 : 19:00:32
If I have a single CASE expression, is it only possible to produce values in one column or can I have multiple values in multiple columns ?
Take the following example:
The following statement will put whatever values in the column aliased as DoINeedHelp...

SELECT
(CASE WHEN needhelp > 'No'
THEN contactsqlteam
ELSE NULL END) AS DoINeedHelp
FROM
i.needhelpfiles

But if I want another column with values from the same condition, do I need to duplicate the CASE statement or is there a means of doing this from within one statement?

i.e - can I do something similar to this (obviously it doesn't work as I have it here):

SELECT
(CASE WHEN needhelp > 'No'
THEN contactsqlteam AS DoINeedHelp, dontknow AS NotTooSure
ELSE NULL END)
FROM
i.needhelpfiles

or is it necessary to do this:

SELECT
(CASE WHEN needhelp > 'No'
THEN contactsqlteam
ELSE NULL END) AS DoINeedHelp,

(CASE WHEN needhelp > 'No'
THEN dontknow
ELSE NULL END) AS NotTooSure
FROM
i.needhelpfiles

Your help is appreciated...
Regards

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-12-03 : 23:42:21
The column alias should come after the END. Its necessary to have two case statements.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-04 : 00:18:39
and if you want like former what you need is IF but in that case you get different resultsets for diff conditions
Go to Top of Page
   

- Advertisement -