Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Case Statement to return multiple columns Expand / Collapse
Author
Message
Posted Monday, September 23, 2013 11:33 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 10, 2013 12:03 AM
Points: 125, Visits: 408
Hi all

am trying to write a case statement an that currently returns one column. Is there any way that it could return multiple columns.

for e.g.

case when <condition> then (Select substring(column, 1, 3)) as Col1, (Select substring(column, 4, 2)) as Col2 
else '' end

Can some one please help. It feels like it's not possible unless I write multiple case statements, but just wanted to check.

Thanks
vani
Post #1497653
Posted Tuesday, September 24, 2013 8:08 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:37 AM
Points: 13,218, Visits: 12,694
vani_r14 (9/23/2013)
Hi all

am trying to write a case statement an that currently returns one column. Is there any way that it could return multiple columns.

for e.g.

case when <condition> then (Select substring(column, 1, 3)) as Col1, (Select substring(column, 4, 2)) as Col2 
else '' end

Can some one please help. It feels like it's not possible unless I write multiple case statements, but just wanted to check.

Thanks
vani


No you can't do that. A case expression is used to help decide the value for a column. Also you don't need subselects inside like that.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1497880
Posted Wednesday, September 25, 2013 12:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 10, 2013 12:03 AM
Points: 125, Visits: 408
Thank you Sean. I thought so.

Cheers
Vani
Post #1498133
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse