NULL value can be dangerous
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.
- Handle through ISNULL function especially when you have some calculations.
example: ISNULL(columnName,0)
- 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
- 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
You must Login to comment.
|
Tue, Apr 28, 2009 11:42 PM
by ahsanm.m
|
This is good one.
|
|
Wed, Apr 29, 2009 1:12 AM
by hasan39
|
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
|
|
Tue, May 5, 2009 1:43 PM
by idgetman05
|
i would suggest for everyone use ISNULL as much as possible amazing function
|
|
Wed, May 6, 2009 12:44 AM
by anish007
|
yeah i also go ISNULL() function
|
|
Thu, May 7, 2009 4:11 AM
by skh21
|
ya it's better 2 use ISNULL
|
|
Tue, May 12, 2009 1:21 AM
by ahmish
|
Its a good awareness to those who happily put Null everywhere :)
|
|
Fri, Aug 14, 2009 8:44 AM
by csharppointer
|
null is nothing actually but really causes serious problems while coding :)
|
|
Wed, Feb 10, 2010 2:10 AM
by cnranasinghe
|
Very useful
|