Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NULL value can be dangerous

0.00/5 (No votes)
1 May 2009 1  
NULL can be produce weird results and sometimes it is hard to diagnose the root cause. So to avoid such a problem like this you need to remember few

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

NULL can be produce weird results and sometimes it is hard to diagnose the root cause. So to avoid such a problem like this you need to remember few things before you write a query.

There are several ways to overcome this solution here I will discuss only 3 solutions.

  1. Handle through ISNULL function especially when you have some calculations.
    example: ISNULL(columnName,0)
  2. Use the Case Statement when you left join or complex statements.
    example: SELECT CASE WHEN Tbl1.Column1=’Value1’ AND Tbl2.Column2=’Value1’ THEN ‘True’ ELSE ‘False’ END as ColumnA
  3. Another way is to handle through COALESCE function, this function will return NULL if the argument is NULL.
    example: SELECT * FROM Table WHERE Column1= COALESCE(@Column1,Column1) AND Column2=@Column2
    In above example Column1 is optional.
    Note: Never use this whenever you performing any operation statements like UPDATE, DELETE etc. better to use in the SELECT Statements.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here