Home > SQL Server Tips > Database Management and Administration > Understanding NULL values in SQL Server
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE MANAGEMENT AND ADMINISTRATION

Understanding NULL values in SQL Server


Barrie Sosinsky, Contributor
11.13.2003
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


A NULL indicates that a value for an entry is not available. This makes a null different from a zero or an empty or blank value. You'll find NULLs valuable when you need to know if a value is either unknown or missing. Many database designers (but not all) eschew the use of NULLs and recommend that you use default values in your tables. It's common practice to substitute the character string "NA" or "N/A" when a value is not available.

NULLS have implications when it comes to grouping, sorting and in particular statistical analysis; but NULLS can still be very useful. One place you can't have NULLS are in primary keys (each value must be unique), or where an identity property for a column must contain a valid value.

SQL Server inserts a NULL into any column in a new row where you don't set the value for that column, and where a default value isn't assigned. For any column with a default value, a NULL is replaced by that default value. You can also set the default value to NULL by using the NULL keyword.

You can set default values in the Enterprise Manager's New Table and Design Table views; and there you can allow or disallow NULLS. If you set a default value for a column, then that default value is always used when there isn't a value specified for entry. Thus, if you allow NULLs, then you will only see NULLs when there is no entry and no default value set, or when you specifically enter the NULL value. For a column where NULLs are disallowed, no data entry or entering a NULL value will create an error condition.

If you use a NULL as part of a query, how NULL values are handled is a function of the SQL-92 ANSI standard. Any NULLs used in a comparison such as =, <, or > will evaluate to FALSE. If the ANSI NULLs option is OFF, then SQL Server will make this comparison, and if a value in both columns is NULL, then the comparison will evaluate to TRUE.


Barrie Sosinsky is president of consulting company Sosinsky and Associates (Medfield MA). He has written extensively on a variety of computer topics.


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Microsoft SQL Server Performance Monitoring and Tuning
Using traces in SQL Server Profiler
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Data restoration and DB property management
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Clearing the Windows page file and its effect on server performance
Optimizing SQL Server indexes –- even when they're not your indexes
Performance implications of transaction log autogrowth in SQL Server
The short course on how SQL Server really works

Database Management and Administration
Password cracking tools for SQL Server
Using traces in SQL Server Profiler
Meet compliance requirements with improved database security practices
Hardening the network and OS for SQL Server security
Securing the server and database in SQL Server
How SQL Server 2008 components impact SharePoint implementations
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Achieving high availability and disaster recovery with SharePoint databases
Clearing the Windows page file and its effect on server performance
Deploying a SQL Server virtual appliance for Microsoft Hyper-V

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
contiguity  (SearchSQLServer.com)
contiguous  (SearchSQLServer.com)
drilldown  (SearchSQLServer.com)
hashing  (SearchSQLServer.com)
hybrid online analytical processing  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts