LIKE

  • Comments posted to this topic are about the item LIKE

    Tom

  • Good question.

    For some reason I thought it could be True because of some weird implicit conversion, so I came up with this code to get my correct answer:

    DECLARE @a CHAR(2);

    DECLARE @b-2 INT;

    SET @a = '1 ';

    SET @b-2 = 1;

    IF @a = @b-2 AND @a LIKE @b-2 AND @b-2 NOT LIKE @a BEGIN

    PRINT 'True';

    END ELSE BEGIN

    PRINT 'False';

    END

    ...and then realized that Tom was asking about same datatype. Right answer for the wrong reason. :hehe:

    --Vadim R.

  • A good idea, but a bit too easy to guess! Even though I failed to come up with an example, I got it right as the obvious answer was 'not possible', therefore for there to be any point asking the question, the answer had to be 'possible'!

  • Toreador (6/24/2010)


    A good idea, but a bit too easy to guess! Even though I failed to come up with an example, I got it right as the obvious answer was 'not possible', therefore for there to be any point asking the question, the answer had to be 'possible'!

    Likewise :ermm:

    _____________________________________________________________________
    [font="Comic Sans MS"]"The difficult tasks we do immediately, the impossible takes a little longer"[/font]

  • Now this will work

    declare @a varchar(16)

    set @a ='a '

    declare @b-2 varchar(16)

    set @b-2 ='a'

    if @a = @b-2 and @a like @b-2 and @b-2 not like @a

    print 'zzz'

  • I knew it had to be trailing spaces. It's always trailing spaces... 😉

    -----
    a haiku...

    NULL is not zero
    NULL is not an empty string
    NULL is the unknown

  • Even though I got it right and know its possible but when executed in SSMS 2005 it returned error.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 3

    Must declare the scalar variable "@a".

    SQL DBA.

  • Toreador (6/24/2010)


    A good idea, but a bit too easy to guess! Even though I failed to come up with an example, I got it right as the obvious answer was 'not possible', therefore for there to be any point asking the question, the answer had to be 'possible'!

    This logic is hilarious!!! It reminds me of the discussion in the Princess Bride between Wesley and the "inconceivable" guy about which glass has the poison in it. 😛

    As funny as it was I followed pretty much the same train of thought.

    _______________________________________________________________

    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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sounds like an interview question. :hehe:

  • SanjayAttray (6/24/2010)


    Even though I got it right and know its possible but when executed in SSMS 2005 it returned error.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 3

    Must declare the scalar variable "@a".

    Code provided in the question is using SQL Server 2008 syntax, that's why it didn't work for you in SQL Server 2005 SSMS.

  • Toreador (6/24/2010)


    A good idea, but a bit too easy to guess! Even though I failed to come up with an example, I got it right as the obvious answer was 'not possible', therefore for there to be any point asking the question, the answer had to be 'possible'!

    Ditto here. 🙂

  • Lynn Pettis (6/24/2010)


    SanjayAttray (6/24/2010)


    Even though I got it right and know its possible but when executed in SSMS 2005 it returned error.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 3

    Must declare the scalar variable "@a".

    Code provided in the question is using SQL Server 2008 syntax, that's why it didn't work for you in SQL Server 2005 SSMS.

    It's surprising how quickly I have come to use SQL 2008 syntax - if I'd written that just a few weeks earlier it would have been in SQL 2000 syntax (so it would have worked in 2000, 2005, and 2008).

    Tom

  • OCTom (6/24/2010)


    Sounds like an interview question. :hehe:

    At least as the first part of a possible two-parter ("So you think it's possible? OK, please give me an example....") :-).

  • Toreador (6/24/2010)


    A good idea, but a bit too easy to guess! Even though I failed to come up with an example, I got it right as the obvious answer was 'not possible', therefore for there to be any point asking the question, the answer had to be 'possible'!

    I wondered whether that reasoning would be common and perhaps lead to a success rate close to 100%. I thought that if the success rate looked too high I could devise a question where the obvious answer was the correct one for my next attempt and see if it attracted a high failure rate. But the success rate turne out to be reasonable - 246 people (about a third of those trying it so far) have got it wrong, so perhaps not too easy after all.

    Tom

  • I've probably been answering too many of these questions recently so always looking for the trick!

Viewing 15 posts - 1 through 15 (of 26 total)

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