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 ««1234»»»

CASE vagaries Expand / Collapse
Author
Message
Posted Monday, September 9, 2013 2:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 12, 2013 11:55 PM
Points: 39, 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'
Post #1492689
Posted Monday, September 9, 2013 3:45 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:33 AM
Points: 2,469, Visits: 2,346
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') + '<'

Post #1492711
Posted Monday, September 9, 2013 5:49 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 1,344, Visits: 1,518
I hate it when we don't have a "none of the above" option.
Post #1492739
Posted Monday, September 9, 2013 5:55 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 11:40 AM
Points: 1,297, Visits: 1,660
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.
Post #1492740
Posted Monday, September 9, 2013 6:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 7:32 AM
Points: 12, Visits: 35
The correct answer is not among the choices...

Post #1492751
Posted Monday, September 9, 2013 6:41 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 3:52 AM
Points: 97, Visits: 114
Thanks Hugo, feeling much better after reading your post. Thought I had Monday morning madness.
Post #1492753
Posted Monday, September 9, 2013 6:52 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 7:34 AM
Points: 1,773, Visits: 2,149
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
Post #1492756
Posted Monday, September 9, 2013 7:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:19 AM
Points: 3,447, Visits: 1,476
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.
Post #1492771
Posted Monday, September 9, 2013 7:29 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, July 25, 2014 8:10 AM
Points: 903, Visits: 1,705
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
Post #1492777
Posted Monday, September 9, 2013 7:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 13,139, Visits: 11,979
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 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 #1492781
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse