You are currently offline, waiting for your internet to reconnect

INFO: Handling of NULLs, Null Strings, and Blanks in SQL Server

This article was previously published under Q45917
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
The following information discusses how NULLS, null strings, andblank strings are handled in SQL Server.
MORE INFORMATION
SQL Server automatically strips trailing blanks from character stringsbefore storing the string if the data type is VARCHAR, or if the datatype is CHAR and nulls are allowed. A trailing blank that is also thefirst character in the string is not stripped. Trailing blanks arepreserved if the data type is CHAR and nulls are not allowed.

If the string contains only blanks (0x20), a single blank is stored.

If the string has zero length and contains nothing at all (that is,""), SQL Server converts it to a single blank before storing it.

The only way to get SQL Server to store a true null is to use the"NULL" keyword in the INSERT or UPDATE statement.

For example, suppose table T1 has a column of type CHAR(10) with nullsallowed, as follows:
   INSERT T1 VALUES (NULL)   <--- Inserts a true null   INSERT T1 VALUES ("")     <--- Inserts a single blank (0x20)   INSERT T1 VALUES ("  ")          ''        ''     ''    ''   INSERT T1 VALUES ("   ")         ''        ''     ''    ''   INSERT T1 VALUES ("    ")        ''        ''     ''    ''				

If the data type is CHAR (fixed length) with nulls allowed, thetrailing blanks that were stripped are not restored by DB-Library(DB-Lib). For example, if a column is defined as CHAR(10) NULL and if('rec1 ') is inserted, the dbdatlen() function will return 4. Ifthe column is defined as CHAR(10) without nulls allowed and the samedata loaded, the dbdatlen() function will return 10.
Transact-SQL dblib Windows NT
Properties

Article ID: 45917 - Last Review: 03/14/2005 17:46:56 - Revision: 1.1

  • Microsoft SQL Server 4.21a Standard Edition
  • kbinfo kbprogramming KB45917
Feedback