Home / ASP.NET Wiki / Security / SQL Server Security / NULL value can be dangerous

NULL value can be dangerous

 Rate It (8)

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.

Revision number 2, Friday, May 01, 2009 10:19:04 PM by rtpHarry

Comments

This is good one.

Your comments " Another way is to handle through COALESCE function, this function will return NULL if the argument is NULL. " is not clear. Actually if all arguments are null the COALSCE return null.If you want to convert null value to any non value then you can use ISNULL. And your note " Note: Never use this whenever you performing any operation statements like UPDATE, DELETE etc. better to use in the SELECT Statements." is not clear to me, pls explain

i would suggest for everyone use ISNULL as much as possible amazing function

yeah i also go ISNULL() function

ya it's better 2 use ISNULL

Its a good awareness to those who happily put Null everywhere :)

null is nothing actually but really causes serious problems while coding :)

Very useful

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)