Difference between Two -ISNULL() and IS NULL

  • Hi Friends,

    I m confused about whether to use IsNull() Function or wether I can simply use @variable IS Not NULL condition.But I am not sure that the condition such as

    IF  @variable IS NOT NULL

    will work properly.

    I want to know which one more efficient  and correct

    If ISNULL(@variable,0) <> 0

     

    OR

    IF @variable Is Not NULL

     

    If anyone knows the difference between two please help me.

     

    Regards

    yuvraj

  • Generally use "is null" in a conditional.

    Use isnull() to change the value from null to something else.

    If null and 0 mean the same thing in your application, then isnull(@var, 0) can make conditionals simpler. But isnull(col, 0) would not use an index on col.

  • This might also be interesting:

    http://qa.sqlservercentral.com/columnists/jtravis/understandingthedifferencebetweenisnull.asp

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • The IS NULL in your example is more efficient and considered standard for this type of check. The reason is when you run

    @var IS NULL

    it evaluates true or false for the condition, but with

    IsNull(@var,0) != 0

    It must first check the condition for the sake of the function, replace the memory with the new value and then eavlauet the true or false of the != condition. So overall more cycles are consumed in the second situation.

    In addition if 0 was ever allowed as a real value then your code would then fail to work properly in that case under the second condition.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply