Sunday, July 13, 2008

What is the result when comparing two nulls in SQL?

The answer is quite interesting with my stuff. Let me clear it in more writting:

1. When we compare two nulls then the result always 'false'. The main reason is the null is not a value its neither an empty nor a empty space, so the actual result is null which places as null.
2. When we compare a null with another which has some value like some int value then the result is false. The actual result is false and not null.

Consider the following examples:

--null = null is null which is false
Declare @intNull1 int
Set @intNull1 =null
Declare @intNull2 int
Set @intNull2=null
If @intNull1=@intNull2
Print 'null = null is true'
Else
Print 'null = null is false'

--Now assign some value
Set @intNull1 = 1
If @intNull1=@intNull2
Print 'null = int value is true'
Else
Print 'null = int value is false'

4 comments:

  1. hi gaurav
    the overall i like these words in this post

    null = null is null which is false

    nobody tells the reason why it is false
    thanks-

    ReplyDelete
  2. Hello Shweta!
    Its my pleasure to have reader like you who noticed this stuff.

    I will try to post more stuffs.

    ReplyDelete
  3. hi Gaurav,
    I really like this post - I hope everyone reading it and appreciates.

    ReplyDelete
  4. Thanks Ajay!

    Thanks to encourage me. Dear send me some real-time problems to hike my morale.

    Moreover, I have gone through your blog, unfortunately I didnt find any post there....

    ReplyDelete