September 7, 2013 at 4:59 pm
Comments posted to this topic are about the item CASE vagaries
September 7, 2013 at 5:05 pm
The question is strange, and the answer is wrong.
"If an ELSE path is not provided in a CASE statement, the value it returns is NULL, not the initial value of the column or expression."
Absolutely tryue (and not vague at all). But not proven by this question. Since @x was not initialized before the SET with the CASE expression, the initial value was NULL, and the code does not enable us to see that it is actually set to NULL and not left unchanged.
The distracters are weird. The last option (NULL) is the only one that at least makes some sense. The second option (1) would have been more believable if it was presented as "@X is now 1<". As for the first option - where would this p come from? I have no idea what reasoning could lead people to believe that this answer can be correct.
And the actual answer is "@X is now A<". That's because the author uses ISNULL instead of the recommended COALESCE. ISNULL does not follow the normal data type precedence rules, but always returns the data type of the first argument. In this case, @X is the first argument, and @X is char(1) - so the result of ISNULL is 'A' ('A NULL' converted / truncated to char(1).)
September 8, 2013 at 1:14 pm
What a weird question 🙂
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
September 8, 2013 at 11:22 pm
First time I have come across a question with an incorrect answer.
A little disappointing for a novice like myself.
September 9, 2013 at 12:24 am
Hugo Kornelis (9/7/2013)
+1
Also: I didn't have any trouble ending up with NULL in @X. As Hugo states, but in other words:
If you set a variable, you have to set it to something. Code with unfilled paths will lead to NULL values.
The thingy about ISNULL() have bitten me before, so I knew what to expect. In fact, I thought that were the caveat to look for in todays question, så I were somewhat miffed when that answer were missing.
September 9, 2013 at 12:29 am
Hugo Kornelis (9/7/2013)The actual answer is "@X is now A<". That's because the author uses ISNULL instead of the recommended COALESCE. ISNULL does not follow the normal data type precedence rules, but always returns the data type of the first argument. In this case, @X is the first argument, and @X is char(1) - so the result of ISNULL is 'A' ('A NULL' converted / truncated to char(1).)
+1
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
September 9, 2013 at 12:43 am
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.
Look at it from the popsitive side - at least the official correct answer was still the "least incorrect" option.
(There have also been questions where the "least incorrect", or sometimes even the "actually correct" option were considered wrong. Most of these will usually be corrected when Steve catches the discussion, so you might not see them f you check old questions now).
Bottom line - if you look at QotD as a way to learn, then always checkk the discussion on the boards!
September 9, 2013 at 1:06 am
Hugo Kornelis (9/7/2013)
The question is strange, and the answer is wrong.As for the first option - where would this p come from? I have no idea what reasoning could lead people to believe that this answer can be correct.
My guess would be that there is some line like SET @x = 'p' missing before the CASE, which would also explain the explanation.
_______________________________________________
www.sql-kefalo.net (SQL Server saveti, ideje, fazoni i fore)
September 9, 2013 at 1:54 am
This was removed by the editor as SPAM
September 9, 2013 at 2:28 am
Very strange question indeed - given that you can only end up with either of two answers, and then the fact that it caters for NULL, you can eliminate that - but still not get an available answer!
Would have been nice to understand the full intention of the question as there are things to learn, as Hugo has explained - Thanks Hugo.
September 9, 2013 at 2:29 am
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'
September 9, 2013 at 3:45 am
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') + '<'
September 9, 2013 at 5:49 am
I hate it when we don't have a "none of the above" option.
September 9, 2013 at 5:55 am
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.
September 9, 2013 at 6:32 am
The correct answer is not among the choices...
Viewing 15 posts - 1 through 15 (of 33 total)
You must be logged in to reply to this topic. Login to reply