wildcards

  • Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    If you only see the like condition and give answer then you will be in those 16 people.

    (but i am not in those 16. I gave answer as "0" . :crazy:)

    What a horrible question...

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Since 'Steppenwolf' is longer than 10 characters, no values are inserted.

    Thus the result of the select is 0.

  • The correct exceution of the query depends on ansi_warnings

    set ansi_warnings off

    DECLARE @i TABLE( mychar VARCHAR(10));

    INSERT @i VALUES ('Steve'), ('Stephan'), ('Stephanie')

    , ('Sterling'), ('Steppenwolf')

    SELECT mychar

    FROM @i

    WHERE mychar LIKE 'Ste[^p]%'

    It returns two rows in ONE resultset.

    The question asks how many results ...?

    The resultset is only ONE.

  • Danny Ocean (9/11/2013)


    Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    If you only see the like condition and give answer then you will be in those 16 people.

    (but i am not in those 16. I gave answer as "0" . :crazy:)

    What a horrible question...

    Sorry I didn`t get it! the problem in this question is the 11 char value in the Insert statement not the LIKE condition!

    I even copy & paste the same batch from the question (JUST TO MAKE SURE I WAS RIGHT NOT DOING THIS ALL THE TIME), got the same error.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • A version of the query without the use of local table:

    SELECT *

    FROM (

    VALUES ('Steve')

    , ('Stephan')

    , ('Stephanie')

    , ('Sterling')

    , ('Steppenwolf')) AS A(mychar )

    WHERE mychar LIKE 'Ste[^p]%'

  • Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    I didn't executed code, Based on the question & answers , i kept correct answer is 2, that's why i got +1, But it is returning error if execute same code in sql so correct answer is 0

  • I am thinking the post was deliberately designed to confuse.

    One can clearly see without even executing the statement that result should be 0 because of string truncation.

    If all strings have length <= 10 then answer is 2.

  • Given the title of the question, I guessed that the 11 character value to be inserted was an oversight! Otherwise great question and nice to learn something new.

  • Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    Simple - always mark your answer before running the code. Any fool can run the code to get a QotD correct. Or not, in this case.

    You will learn far more from trying to figure out the answer and getting it wrong than running the code and getting it right.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (9/11/2013)


    Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    Simple - always mark your answer before running the code. Any fool can run the code to get a QotD correct. Or not, in this case.

    You will learn far more from trying to figure out the answer and getting it wrong than running the code and getting it right.

    Agree - I never thought QOTD as being an exercise in copy and paste.

    Yes the code does throw an error when run however I get the following when running the script:-

    (1 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    (1 row(s) affected)

    Msg 8152, Level 16, State 14, Line 7

    String or binary data would be truncated.

    The statement has been terminated.

    mychar

    ----------

    Steve

    Sterling

    (2 row(s) affected)

    -------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
    There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
    I would never join a club that would allow me as a member - Groucho Marx

  • ChrisM@Work (9/11/2013)


    Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    Simple - always mark your answer before running the code. Any fool can run the code to get a QotD correct. Or not, in this case.

    You will learn far more from trying to figure out the answer and getting it wrong than running the code and getting it right.

    Indeed. As if I'm going to count the number of characters in Steppenwolf.

    Nice basic question Steve, thanks.

    Born to be wild!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • ChrisM@Work (9/11/2013)


    Hany Helmy (9/11/2013)


    I am really amazed of the 16 people (as of now) who got it right! :w00t:

    Hope one of them will post a reply explaining how he got the result without throwing an error.

    Simple - always mark your answer before running the code. Any fool can run the code to get a QotD correct. Or not, in this case.

    You will learn far more from trying to figure out the answer and getting it wrong than running the code and getting it right.

    I will quote it from another post: "One can clearly see without even executing the statement that result should be 0 because of string truncation", so without running the code, the result will be 0 because of the 11 character value; Simple As That!

    And by the way I copied the code just because I could`t believe I got it wrong, so I wanted to make sure what and where was my mistake, of course not doing this every time!

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Koen Verbeeck (9/11/2013)


    Indeed. As if I'm going to count the number of characters in Steppenwolf.

    Why wouldn't you? I did, and got it wrong as a result.

    I was guessing though, as I didn't know whether the invalid value would prevent all the others being inserted or not.

  • This was removed by the editor as SPAM

  • kapil_kk (9/10/2013)


    Danny Ocean (9/10/2013)


    handkot (9/10/2013)


    I got an error: "String or binary data would be truncated. The statement has been terminated."

    (0 row(s) affected);-)

    Just because of "Steppenwolf" having length 11. So insert statement is not executed successfully.

    Correct answer should be "0".

    :crazy:

    I think, nobody check this question before post on SSC. This question is completely wrong.

    Still i didn't believe that this question is posted by "Steve Jones - SSC Editor". :w00t:

    +1

    Totally agree:w00t:

    +1

Viewing 15 posts - 16 through 30 (of 145 total)

You must be logged in to reply to this topic. Login to reply