LIKE

  • I figured it was possible because of trailing spaces, and tested it, but I messed my test rig up, so I got it wrong... 🙁

    Thanks for the question!

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

    Just to make the difference plain.... SQL 2005 requires a separate SET or SELECT to establish the value of a local variable.

    declare @a varchar(6); set @a ='a ';

    declare @b-2 varchar(6); select @b-2 = 'a';

    if @a = @b-2 and @a like @b-2 and @b-2 not like @a print 'zzz'

  • This is what I absolutely hate about SQL Server. A string is a f**king string, no matter whether it's got spaces on it at the end.

    Jeez.

  • good question ... thanks...:-)

  • Richard Tatterton (6/24/2010)


    This is what I absolutely hate about SQL Server. A string is a f**king string, no matter whether it's got spaces on it at the end.

    Jeez.

    While this is true.... a string is a string, but the deviation here is the method used to evaluate the strings. Part of the ANSI standard is to make strings evaluated with "=" the same length before comparison and this is not a requirement for pattern matching. Like uses pattern matches so the string does not have to be equal.

  • Richard Tatterton (6/24/2010)


    This is what I absolutely hate about SQL Server. A string is a f**king string, no matter whether it's got spaces on it at the end.

    Jeez.

    I'll bet you'd hate it more if it didn't offer such a simple way to include a specific number of trailing spaces in a match comparison.

    If I'm looking for all rows where firstname is 'John ' (with three trailing blanks), how would I code that? Comparing with "=" sure doesn't do it. Both of these queries return true:

    Declare @firstname varchar(10)

    set @firstname = 'John '

    select With_Spaces_EQ = case when

    @firstname = 'John '

    then 'True' else 'Not True' end

    Set @firstname = 'John'

    select No_Spaces_EQ = case when

    @firstname = 'John '

    then 'True' else 'Not True' end

    One way around this would seem be to add another condition teting DATALENGTH (assume @firstname is still defined):

    set @firstname = 'John '

    select With_Spaces_DataLen = case when

    @firstname = 'John ' and DATALENGTH(@firstname) >= 7

    then 'True' else 'Not True' end

    Set @firstname = 'John'

    select No_Spaces_DataLen = case when

    @firstname = 'John ' and DATALENGTH(@firstname) >= 7

    then 'True' else 'Not True' end

    ... but that seems rather strained and/or clumsy.

    I'd rather code a pattern match with LIKE:

    Set @firstname = 'John '

    select With_Spaces_Like = case when

    @firstname LIKE 'John '

    then 'True' else 'Not True' end

    Set @firstname = 'John'

    select No_Spaces_Like = case when

    @firstname LIKE 'John '

    then 'True' else 'Not True' end

  • Adam Haines (6/25/2010)


    While this is true.... a string is a string, but the deviation here is the method used to evaluate the strings. Part of the ANSI standard is to make strings evaluated with "=" the same length before comparison and this is not a requirement for pattern matching. Like uses pattern matches so the string does not have to be equal.

    Maybe it is the ANSI standard to do so, in which case I expand my dislike from SQL Server to ANSI. I seriously cannot understand how an implicit truncation of spaces is helpful. Like I said, a string is a string, whether the characters are ascii 32 or not. It doesn't take much to truncate the trailing spaces yourself if you need to do it.

    Most (if not all) programming languages I've developed in so far in my 30-odd years have followed this procedure, and it works well. Not only well, but it's just intuitively correct, unlike this.

  • Richard Tatterton (6/25/2010)


    Adam Haines (6/25/2010)


    While this is true.... a string is a string, but the deviation here is the method used to evaluate the strings. Part of the ANSI standard is to make strings evaluated with "=" the same length before comparison and this is not a requirement for pattern matching. Like uses pattern matches so the string does not have to be equal.

    Maybe it is the ANSI standard to do so, in which case I expand my dislike from SQL Server to ANSI. I seriously cannot understand how an implicit truncation of spaces is helpful. Like I said, a string is a string, whether the characters are ascii 32 or not. It doesn't take much to truncate the trailing spaces yourself if you need to do it.

    Most (if not all) programming languages I've developed in so far in my 30-odd years have followed this procedure, and it works well. Not only well, but it's just intuitively correct, unlike this.

    If you've been at this game for three decades, then you must have used COBOL. Think of how that language handles trailing spaces in comparisons. Or is it not one of the "Most...languages"? For these data:

    01 String-Fields.

    05 String-1 Pic X(20) Value 'Hello'.

    05 String-2 Pic X(40) Value 'Hello '.

    ... would you not expect a this to come back as "True"?

    If String-1 = String-2

    Display "True"

    Else Display "Not a match".

    Of course, it resolves as "True" due to implicit padding of the shorter element to match the length of the longer one. It seems to me to be pretty much the same thing that MS SQL does.

    It's the "LIKE" operation that's a bit different, and it is different by design with good reason, as I think I demonstrated in my previous post. If you don't like "LIKE", so be it, but that hardly seems a logical reason to bash SQL syntax and string-handling standards that actually are pretty much the same as those in other languages.

  • john.arnott (6/25/2010)


    If you've been at this game for three decades, then you must have used COBOL. Think of how that language handles trailing spaces in comparisons. Or is it not one of the "Most...languages"? For these data:

    01 String-Fields.

    05 String-1 Pic X(20) Value 'Hello'.

    05 String-2 Pic X(40) Value 'Hello '.

    ... would you not expect a this to come back as "True"?

    If String-1 = String-2

    Display "True"

    Else Display "Not a match".

    Of course, it resolves as "True" due to implicit padding of the shorter element to match the length of the longer one. It seems to me to be pretty much the same thing that MS SQL does.

    It's the "LIKE" operation that's a bit different, and it is different by design with good reason, as I think I demonstrated in my previous post. If you don't like "LIKE", so be it, but that hardly seems a logical reason to bash SQL syntax and string-handling standards that actually are pretty much the same as those in other languages.

    Yes, I've spent quite a few years in COBOL/Assembler but to be honest, I can't remember having string comparisons in COBOL (I was mainly on Assembler) - it was nearly 20 years ago. The fact remains that a string is a string and why should any compiler/interpreter tell you (the programmer) what the string is? I know what the string is; it's got a few spaces at the end, which I need to know about.

  • Good question, thanks.

    I got the question right for a reason close to, but not the explanation, so I learned a bit also.

    As usual I also enjoy the debate in the discussion.

  • Excellent QOTD and discussion. Special thanks to john.arnott for his explanatory code samples.

  • Thanks for the QOtD

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 12 posts - 16 through 26 (of 26 total)

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