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

Question of the Day for 10 Mar 2006 Expand / Collapse
Author
Message
Posted Tuesday, March 7, 2006 12:05 PM
SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, May 18, 2007 3:36 PM
Points: 10,039, 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.
Post #263946
Posted Friday, March 10, 2006 2:16 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!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...
Post #264743
Posted Friday, March 10, 2006 3:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:21 AM
Points: 6,098, Visits: 8,367

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
Post #264752
Posted Friday, March 10, 2006 12:29 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135

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.

Post #264978
Posted Thursday, November 22, 2012 3:02 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:12 AM
Points: 1,953, Visits: 2,397
nice and tricy one...
thanks Hugo for the explanation



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1387798
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse