SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


LIKE


LIKE

Author
Message
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3670 Visits: 3059
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


Richard Tatterton
Richard Tatterton
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 39
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.
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3670 Visits: 3059
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.
Richard Tatterton
Richard Tatterton
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 39
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.
Daniel Bowlin
Daniel Bowlin
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8194 Visits: 2629
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.
Rose Bud
Rose Bud
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1795 Visits: 1062
Excellent QOTD and discussion. Special thanks to john.arnott for his explanatory code samples.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68393 Visits: 18570
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search