SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


case statement puzzle


case statement puzzle

Author
Message
amit_adarsh
amit_adarsh
SSC Eights!
SSC Eights! (994 reputation)SSC Eights! (994 reputation)SSC Eights! (994 reputation)SSC Eights! (994 reputation)SSC Eights! (994 reputation)SSC Eights! (994 reputation)SSC Eights! (994 reputation)SSC Eights! (994 reputation)

Group: General Forum Members
Points: 994 Visits: 169
Comments posted to this topic are about the item case statement puzzle
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)

Group: General Forum Members
Points: 146312 Visits: 13350
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?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
sharath.chalamgari
sharath.chalamgari
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3216 Visits: 798
Nice Question,:-)

as you said once the condition is satisfied it will not look at other statement.
Andrew Watson-478275
Andrew Watson-478275
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2926 Visits: 2864
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 Pinch.
DugyC
DugyC
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2278 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"
Ujar
Ujar
SSC-Addicted
SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)SSC-Addicted (407 reputation)

Group: General Forum Members
Points: 407 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..
Steve Jones
Steve Jones
SSC Guru
SSC Guru (330K reputation)SSC Guru (330K reputation)SSC Guru (330K reputation)SSC Guru (330K reputation)SSC Guru (330K reputation)SSC Guru (330K reputation)SSC Guru (330K reputation)SSC Guru (330K reputation)

Group: Administrators
Points: 330707 Visits: 20112
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
My Blog: www.voiceofthedba.com
ken.trock
ken.trock
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2732 Visits: 1737
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 Exclamation
brdudley
brdudley
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1860 Visits: 1776
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"
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: General Forum Members
Points: 144975 Visits: 18651
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, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

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