|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, January 22, 2011 8:42 PM
Points: 17,
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.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, January 22, 2011 8:42 PM
Points: 17,
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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:10 PM
Points: 2,673,
Visits: 2,418
|
|
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:42 AM
Points: 1,072,
Visits: 1,026
|
|
| Excellent QOTD and discussion. Special thanks to john.arnott for his explanatory code samples.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 18,754,
Visits: 12,337
|
|
|
|
|