Thanks for your responses,

Surely there is a plethora of bugs keeping devs busy where someone has failed to consider the null case in a < > comparison which has unintended results ?!

Especially considering the following c# code

|int? x = null;
if(x != 1)
    Console.WriteLine("x is not 1");
else
    Console.WriteLine("x is 1");|

and the 'same' thing in t-sql does the opposite...



On 18/01/2012 4:52 PM, Jano Petras wrote:
Hi Wallace,

I am always using ISNULL function for these cases when I am to compare.

Another possibility is to check for NULL explicitly:

IF @var IS NULL BEGIN
  -- do stuff
END
ELSE BEGIN
  -- do other stuff
END


Cheers,
jano
On 18 January 2012 08:20, Wallace Turner <[email protected] <mailto:[email protected]>> wrote:

    Consider:

    |declare @var bit

    if (select @var)<>  1
      print 'value is not equal to 1'
    else
    print 'value is 1'
    |

    assuming ANSI_NULLS is on then the output is:

        value is 1

    What are some common ways that people deal with this 'gotcha' ? eg
    1) don't use <> operator at all
    2) use ISNULL

    Please consider the case where the select is a (more) complex
    statement.


    Regards

    Wal


Reply via email to