Substring comprehension

  • Comments posted to this topic are about the item Substring comprehension

  • 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?

  • 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

    😉

  • 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...



    See, understand, learn, try, use efficient
    © Dr.Plch

  • 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.

  • I chose the one closest to the answer and I seem to have got it right. Hihahhhhh

    What you don't know won't hurt you but what you know will make you plan to know better
  • Can some one explains how it returns 'chris' when the start value is -6 and length is 12.

  • sorry, wrong post..

  • 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.

  • A empty not null. Substring returns null only if expression is null.

  • 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. 😀


    Have Fun!
    Ronzo

  • As some previous commenters did, I got this right by guessing what the questioner had got wrong.

    Tom

  • 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