Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

LIKE Expand / Collapse
Author
Message
Posted Friday, June 25, 2010 5:13 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
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

Post #943386
Posted Friday, June 25, 2010 7:58 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 22, 2011 8:42 PM
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.
Post #943399
Posted Friday, June 25, 2010 9:31 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
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.
Post #943402
Posted Friday, June 25, 2010 10:07 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 22, 2011 8:42 PM
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.
Post #943403
Posted Monday, June 28, 2010 12:29 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 9:52 AM
Points: 2,818, Visits: 2,549
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.
Post #944157
Posted Monday, June 28, 2010 2:49 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, May 28, 2014 1:04 PM
Points: 1,313, Visits: 1,055
Excellent QOTD and discussion. Special thanks to john.arnott for his explanatory code samples.
Post #944250
Posted Wednesday, July 7, 2010 3:01 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:20 AM
Points: 21,091, Visits: 14,762
Thanks for the QOtD



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #948865
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse