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

Last reviewed: April 25, 1997
Article ID: Q45917

The information in this article applies to:
  • Microsoft SQL Server version 4.2 for OS/2
  • Microsoft SQL Server, version 4.2

SUMMARY

The following information discusses how NULLS, null strings, and blank strings are handled in SQL Server.

MORE INFORMATION

SQL Server automatically strips trailing blanks from character strings before storing the string if the data type is VARCHAR, or if the data type is CHAR and nulls are allowed. A trailing blank that is also the first character in the string is not stripped. Trailing blanks are preserved 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 nulls allowed, 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, the trailing 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. If
the column is defined as CHAR(10) without nulls allowed and the same data loaded, the dbdatlen() function will return 10.


Additional query words: Transact-SQL dblib Windows NT
Keywords : kbprg SSrvDB_Lib SSrvTran_SQL SSrvWinNT
Version : 4.2 | 4.2
Platform : OS/2 WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 25, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.