|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 18, 2007 3:36 PM
Points: 10,040,
Visits: 1
|
|
| Comments posted to this topic are about the Question of the Day for 10 Mar 2006 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=725.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Tuesday, August 10, 2010 4:54 AM
Points: 815,
Visits: 32
|
|
Well I learned something... I tried comparing the results of the following in QA: SELECT LEN(' ') returns 0 (all blanks are trimmed from the expression) SELECT DATALENGTH(' ') returns 6 (6 blanks) SELECT LEN(' a ') also returns 6 (the a is the 6th character, the trailing blank is trimmed) SELECT DATALENGTH(' a ') returns 7 (the trailing blank is included) but why does if '' = char(32) select 'Yes' return Yes ? The fact that the string '' evaluates to something other than NULL causes me problems when my application removes a value from a field, because the field does not return to NULL, it returns to '', so I cannot use IS NULL when searching. I have the same problem with dates, SQL Server seems to set the date field to -1 if the value in the field is cleared, giving me a date value of 31-Dec-1899, much to the bemusement of my users. David
If it ain't broke, don't fix it...
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:22 AM
Points: 5,244,
Visits: 7,059
|
|
why does if '' = char(32) select 'Yes' return Yes ? That's a result of how ANSI defines string comparisons. If two strings are of unequal length, ANSI says that the shorter string is first padded with spaces to match the length of the longer one, then the actual comparison (character by character) is carried out. So '' (length 0) is padded to the length of CHAR(32) )(length 1) by adding one space - which is the same character as CHAR(32). After this padding, the strings coompare equal. (Note: technically, ANSI also allows for a string comparison without padding; the collation dictates which method has to be used. For backwardsd compatibility reasons, MS SQL Server (and AFAIK all other major DB's) still offer only the method with padding) The fact that the string '' evaluates to something other than NULL causes me problems when my application removes a value from a field, because the field does not return to NULL, it returns to '', so I cannot use IS NULL when searching. I have the same problem with dates, SQL Server seems to set the date field to -1 if the value in the field is cleared, giving me a date value of 31-Dec-1899, much to the bemusement of my users. You'll have to make sure that your frontend sends NULL to the server when your users delete the contents of a field. For instance, if you use Enterprise Manager to edit data (which I really should recommend against!), then you can hit Ctrl-0 in any field to set it to NULL, whereas simply deleting the contents of a char field would set it to '' (the empty string). -- Hugo Kornelis (SQL Server MVP)
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:22 PM
Points: 1,276,
Visits: 1,112
|
|
LEN() strips trailing pad characters (normally spaces, but could vary depending on collation) and DATALENGTH() does not. '' isn't the same thing as NULL. To solve your problem, you might check into NULLIF(), or force a NULL value via an UPDATE statement. For example, NULLIF('', '') returns NULL. P.S., you could also create a TRIGGER to force the value to NULL if it is '' at insert or update time.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
nice and tricy one... thanks Hugo for the explanation
|
|
|
|