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