The first thing you need to understand is that NULL in a database column has a meaning: it means that column was not populated. NULLs are not evil, but they are misunderstood. Would it be right for me to let you do math on a NULL, or use it as a key for another table? No, and that's by design.
The thing that burns a lot of people is that NULL is NEVER equal to anything, not even another NULL.
If you are working with a column that MUST be populated in every case, then you put a NOT NULL constraint on it. Nobody will ever be able to insert NULL into that column.
What if you are trying to read a column that is supposed to allow NULL values? There are several ways of testing for null. This example is for a SqlDataReader (C# syntax, but you can easily convert if needed):
Test before reading:
string s;
if (rdr.IsDbNull(0))
s = "NULL";
else
s = rdr.GetString(0);
Or, if you want to read the value as a string, this will automatically convert a NULL to an empty string:
rdr["col_name"].ToString(); // return empty string for NULL
Or use the SQL function ISNULL to translate a NULL value to something else from within SQL Server:
select isnull(CategoryID,0) from ...
Eric
|