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 12»»

case statement puzzle Expand / Collapse
Author
Message
Posted Tuesday, May 25, 2010 9:35 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Monday, August 25, 2014 10:25 PM
Points: 698, Visits: 158
Comments posted to this topic are about the item case statement puzzle
Post #927926
Posted Wednesday, May 26, 2010 12:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:13 AM
Points: 13,638, Visits: 10,525
Nice question. I didn't know that constants (like 1) could be placed after CASE, so I definately learned something today.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #927974
Posted Wednesday, May 26, 2010 1:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:29 AM
Points: 1,179, Visits: 786
Nice Question,

as you said once the condition is satisfied it will not look at other statement.
Post #927996
Posted Wednesday, May 26, 2010 3:25 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 3:53 AM
Points: 1,223, Visits: 2,211
I really need to stop being so mistrusting. The right answer was so obvious, I assumed it had to be a trick question and got it wrong .
Post #928055
Posted Wednesday, May 26, 2010 3:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 27, 2013 4:28 AM
Points: 1,248, Visits: 779
Good question that in its simplicity makes you question what seems the obvious answer and think about it more deeply.

According to SQL Books Online, the parser actually checks all conditions and returns the first true condition, if none then returns "else" if its coded otherwise NULL. That's how it seems to be described.


_____________________________________________________________________
"The difficult tasks we do immediately, the impossible takes a little longer"
Post #928059
Posted Wednesday, May 26, 2010 7:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 12, 2010 4:24 AM
Points: 41, Visits: 102
Really a nice question..

I have got the confusion that, we need to declare the variable name followed by case statement and after that the value followed by when.

Thanks..

Post #928282
Posted Wednesday, May 26, 2010 9:13 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 7:06 AM
Points: 33,204, Visits: 15,355
This one reminds me of the fundamental concepts I learned in C. 0=false, 1=true.






Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #928364
Posted Wednesday, May 26, 2010 9:24 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 25, 2014 3:02 PM
Points: 323, Visits: 1,459
da-zero (5/26/2010)
Nice question. I didn't know that constants (like 1) could be placed after CASE, so I definately learned something today.


Yeah, same here. Good thing a 4th choice like "error" wasn't supplied with the question or I might have gotten it wrong
Post #928380
Posted Wednesday, May 26, 2010 10:35 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 8:15 AM
Points: 1,390, Visits: 1,555
Steve Jones - Editor (5/26/2010)
This one reminds me of the fundamental concepts I learned in C. 0=false, 1=true.


If you are saying that it chooses the first one because 1 = true, that is not correct. It is using the Case syntax similar to a Switch statement. The Select is equivalent to

Select Case
When @flg = 1 then 'True'
When @flg = 1 then 'False'
Else 'No Flag'


Try changing the constant or the value of @flg. It will result in "No Flag"
Post #928451
Posted Wednesday, May 26, 2010 11:15 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Friday, August 29, 2014 1:51 PM
Points: 21,644, Visits: 15,317
Interesting. I did not know that a case could be written this way as well.

Thanks.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #928476
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse