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


CASE vagaries


CASE vagaries

Author
Message
akondok
akondok
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 14
DECLARE @x CHAR(1)

SET @x = CASE
WHEN @x = 'x' THEN 1
END



PRINT '@X is now ' + CAST(ISNULL(@x, 'A NULL') AS VARCHAR(50)) + '<'

The correct answer should be @X is now B<
This is because the value of @x after the SET statement is NULL. The ISNULL function attempts to assign the value 'A NULL' to @x but @x is CHAR(1) thus it is truncated to 'A'
Carlo Romagnano
Carlo Romagnano
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12315 Visits: 3514
Maybe, the author exchanged the CAST and the ISNULL:
The right version:

DECLARE @x CHAR(1)

SET @x = CASE
WHEN @x = 'x' THEN 1
END

PRINT '@X is now ' + ISNULL(CAST(@x AS VARCHAR(50)), 'A NULL') + '<'


batgirl
batgirl
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: 3211 Visits: 1820
I hate it when we don't have a "none of the above" option.
sknox
sknox
SSCertifiable
SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)

Group: General Forum Members
Points: 6919 Visits: 3158
david.gwalter (9/8/2013)
First time I have come across a question with an incorrect answer.
A little disappointing for a novice like myself.


I find that even for questions that are well and correctly written, the discussion holds additional information. So it's almost always worthwhile to read the discussion, even if the question is poorly written. I treat the questions as more of an introduction to a topic of the day.

I also try to keep in mind that the questions are written by the community, and so will reflect the varying levels of knowledge and skill out there.

Having said that, question authors should at least run the code they write and make sure that their answer is correct in that respect.
james.a.waage
james.a.waage
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 74
The correct answer is not among the choices...
Paul Andrew
Paul Andrew
SSC Veteran
SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)SSC Veteran (252 reputation)

Group: General Forum Members
Points: 252 Visits: 153
Thanks Hugo, feeling much better after reading your post. Thought I had Monday morning madness.
Thomas Abraham
Thomas Abraham
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6029 Visits: 2256
I appreciate the effort of the OP to submit this question.

However, I don't see how the OP could have thought that one of the answers given was correct, had they actually run the code.

Hopefully we all learned something today, and the OP's next question will be better.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
Scott Arendt
Scott Arendt
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5483 Visits: 1733
Look at it from the positive side - at least the official correct answer was still the "least incorrect" option.

Yep, that's why I chose it. When in doubt choose the least incorrect answer.
Julie Breutzmann
Julie Breutzmann
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2373 Visits: 1987
sknox (9/9/2013)
david.gwalter (9/8/2013)
First time I have come across a question with an incorrect answer.
A little disappointing for a novice like myself.


I find that even for questions that are well and correctly written, the discussion holds additional information. So it's almost always worthwhile to read the discussion, even if the question is poorly written. I treat the questions as more of an introduction to a topic of the day.

I also try to keep in mind that the questions are written by the community, and so will reflect the varying levels of knowledge and skill out there.

Having said that, question authors should at least run the code they write and make sure that their answer is correct in that respect.

+1
Sean Lange
Sean Lange
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: General Forum Members
Points: 147864 Visits: 18567
I suspect that the intention was to demonstrate that ISNULL will pick up the first datatype in the list which in this case is char(1). Therefore the value of returned from the isnull would only be the first character in the longer string. It is interesting that the title refers to CASE when what is seems the user is evaluating is the knowledge of how ISNULL works.

_______________________________________________________________

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 Modens 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)
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