Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SELECT list block with CASE?


SELECT list block with CASE?

Author
Message
SQL Mad Rafi
SQL Mad Rafi
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 363
is something like this possible?

SELECT

CASE WHEN (@X = 1)
THEN
COLUMN1,
COLUMN2,
COLUMN3
ELSE
COLUMN4,
COLUMN5,
COLUMN6,
END

FROM TABLE1
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19016
No, but you can cheat:

DECLARE @X TINYINT = 0

;WITH SampleTable AS (
SELECT SOMECOLUMNS = 'SOMECOLUMNS', COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3',
COLUMN4 = 'COLUMN4', COLUMN5 = 'COLUMN5', COLUMN6 = 'COLUMN6'
)
SELECT SOMECOLUMNS, x.*
FROM SampleTable
CROSS APPLY (
SELECT COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3' WHERE @X = 1
UNION ALL
SELECT COLUMN4, COLUMN5, COLUMN6 WHERE @X <> 1
) x



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Adi Cohn-120898
Adi Cohn-120898
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2224 Visits: 6076
One simple way is to use a if block:

If @x=1
Select column1, column2, column3 from MyTable
Else
Select column4, column5, column6 from MyTable



If you insist of doing just one select statement, then if you don't mind that the columns will have the same name regardless of the @x's value, you can use this code:


select case when @x = 1 THEN column1 else column4 end as col1,
case when @x = 1 THEN column2 else column5 end as col2,
case when @x = 1 THEN column3 else column6 end as col3
from MyTable



Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
jshahan
jshahan
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 1995
DECLARE @X TINYINT = 0

;WITH SampleTable AS (
SELECT SOMECOLUMNS = 'SOMECOLUMNS', COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3',
COLUMN4 = 'COLUMN4', COLUMN5 = 'COLUMN5', COLUMN6 = 'COLUMN6'
)
SELECT SOMECOLUMNS, x.*
FROM SampleTable
CROSS APPLY (
SELECT COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3' WHERE @X = 1
UNION ALL
SELECT COLUMN4, COLUMN5, COLUMN6 WHERE @X <> 1
) x


ChrisM@Work, please help educate me. There is something very simple here I don't understand.

How does CROSS APPLY join SampleTable to x?
Dung Dinh
Dung Dinh
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 1231
Maybe, you can use a dynamic query

DECLARE @X bit, @strSQL nvarchar(2000)
SET @X = 1

SET @strSQL =' SELECT ' + CASE WHEN @X = 1 THEN ' COLUMN1,COLUMN2,COLUMN3'
ELSE ' COLUMN4,COLUMN5,COLUMN6' END
+ ' FROM TABLE1 '

EXEC (@strSQL)


ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19016
jshahan (3/5/2013)
DECLARE @X TINYINT = 0

;WITH SampleTable AS (
SELECT SOMECOLUMNS = 'SOMECOLUMNS', COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3',
COLUMN4 = 'COLUMN4', COLUMN5 = 'COLUMN5', COLUMN6 = 'COLUMN6'
)
SELECT SOMECOLUMNS, x.*
FROM SampleTable
CROSS APPLY (
SELECT COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3' WHERE @X = 1
UNION ALL
SELECT COLUMN4, COLUMN5, COLUMN6 WHERE @X <> 1
) x


ChrisM@Work, please help educate me. There is something very simple here I don't understand.

How does CROSS APPLY join SampleTable to x?


The second to last line of my sig is a link to Paul White's famous APPLY articles. Well worth a read. APPLY without a table reference simply means calculate this and affects, and returns results to, the "current row".

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
jshahan
jshahan
Old Hand
Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)Old Hand (391 reputation)

Group: General Forum Members
Points: 391 Visits: 1995
Thanks for the response and the reference. I've got to wrap my brain around this. Been driving me nuts.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search