1

When I check if a value is NULL I usually use '=', This doesn't work somehow but 'is null' works. Why's that? What is the difference?

vhadalgi
  • 7,027
  • 6
  • 38
  • 67
moritzg
  • 4,266
  • 3
  • 37
  • 62

3 Answers3

1

Have a look at : SQL is null and = null

Basically, null can be thought of as no value or no value know. A comparison operator (eg =) is effectively asking if an unknown or non existent value is equal to another unknown or non existent value. The only sensible return in this case is another unknown or non existent value - null.

IS NULL asks is the value unknown or non existent - this can return either true or false.

Community
  • 1
  • 1
1
WHERE myvalue = null 

will never be true. Since even if "myvalue" is undefined ("null"), the database can't determine that it equals null (since null by definition means "unknown" and two "unknowns" can't be equal to each other).

WHERE myvalue IS null 

is more straight-forward. This checks whether "myvalue" is undefined (is "null" in database lingo).

Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
  • Funny, thanks, I come from Java so it sounded reasonable to me that I check for NULL with '='... – moritzg Dec 17 '13 at 06:59
0

Please see this

http://technet.microsoft.com/en-us/library/ms188048.aspx

you can try the below code. THis is for SQL SEVER

SET ANSI_NULLS OFF

DECLARE @VALUE INT =NULL IF @VALUE=NULL PRINT 'IN 1' IF @VALUE IS NULL PRINT 'IN 2'

Ashutosh Arya
  • 1,138
  • 2
  • 8
  • 14