Bookmark and Share

Thursday, November 08, 2007

Little question on sql... Null or not null?

Executing this TSQL code, what will be the result? "True or False"  or  "Not defined?"

if 'a' <> null or 'a' = null 
    print 'true or false!'
else 
    print 'not defined...!'

Answer: the first condition is neither true nor false, is… undefined.
Don’t compare columns using the ‘= null’ or ‘<> null’, pay attention, the results should be very different from what you expect.
You need to use ‘IS NULL’ and ‘IS NOT NULL’, these are the correct operators for managing null values in Sql server.

if null <> null or null = null 
    print 'true or false!'
else 
    
print 'not defined...!'

Same story for the above statement, the result is… “Not defined!”

Matteo

">">Site Feed

No comments:

Post a Comment