October 31, 2009 at 1:20 pm
Comments posted to this topic are about the item Substring comprehension
November 1, 2009 at 3:46 am
November 2, 2009 at 12:31 am
Took a guess and got it wrong, but...
Ran this on 2005 and A is not actually NULL, its an empty string. Maybe its NULL on 2008?
November 2, 2009 at 1:07 am
The correct answer is: A is '' (empty string), B is 'Chris'.
Try this code:
declare @name varchar(12)
select @name = 'Christopher'
select case when substring(@name,15,12) is null then 'NULL' else 'NOT NULL' end as A, substring(@name,-6,12) as B
and you will see the result:
A B
-------- ------------
NOT NULL Chris
(1 row(s) affected)
Despite what is shown in 2008 BOL
Maybe the author of the question uses some "secret special" BOL 😉
I use the following link: http://msdn.microsoft.com/en-us/library/ms187748.aspx, and what I see there:
SUBSTRING ( value_expression ,start_expression , length_expression )
...
If length_expression is negative, an error is generated and the statement is terminated.
So, length_expression cannot be negative, but the "-6" from the question is start_expression.
the code ... does not return the whole value expression when the sum of the start and length values are greater than the length of the variable
Just a little detail from BOL:
the whole value expression beginning at start_expression is returned
😉
November 2, 2009 at 5:14 am
Just a little mistake, empty string and null.
But to tell the truth, I was confused, because I know the first expression returns empty string and not a null...
November 2, 2009 at 7:15 am
Even though A is empty string versus Null the answer is still obvious given that it is multiple choice. In other words it was obvious that the author had just made a mistake between empty/null since options A and C were so far off.
November 3, 2009 at 12:30 am
I chose the one closest to the answer and I seem to have got it right. Hihahhhhh
November 3, 2009 at 7:09 am
Can some one explains how it returns 'chris' when the start value is -6 and length is 12.
November 3, 2009 at 8:04 am
sorry, wrong post..
November 3, 2009 at 10:00 am
If you think of it sort of as a number line, the name "Christopher" would occupy spots 1 thru 11 of the line. The expression is askiing you to look at spots -6 thru 5. This is 12 spots because of the number zero. The first 7 spots (-6 thru 0) we're asked to look at don't correspond to any characters in the name and don't return any characters. But the last 5 spots correspond to spots 1 thru 5 of the number line and those spots are occupied by the characters "Chris".
This is perhaps a clumsy example, but it seems to work.
November 6, 2009 at 4:54 pm
A empty not null. Substring returns null only if expression is null.
November 9, 2009 at 10:53 am
Toby White (11/2/2009)
Even though A is empty string versus Null the answer is still obvious given that it is multiple choice. In other words it was obvious that the author had just made a mistake between empty/null since options A and C were so far off.
So...
Always assume that there is a right answer, and if none of the above is not shown, try and figure out which answer would be right assuming the author made a mistake.
Don't like this logic at all. It may result in your passing the test, but it requires you to try and figure out what the author "meant" the answers to be.
I used logic that went like this... if you know two of the answers are wrong, go with the one that might be right (i.e. error). Of course my "logic" failed and yours succeeded, this time. 😀
November 13, 2009 at 2:01 pm
As some previous commenters did, I got this right by guessing what the questioner had got wrong.
Tom
November 18, 2009 at 7:13 am
Toby White (11/2/2009)
Even though A is empty string versus Null the answer is still obvious given that it is multiple choice. In other words it was obvious that the author had just made a mistake between empty/null since options A and C were so far off.
Except that A is not so far off. I thought along this line: this first select returns the empty string, I don't know what the second one does, but no answer has "this first select returns the empty string", so the only compatible answer is A: error.
Not nice.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply