NULL is equal to a number???

  • declare @a int,@B int

    select @b-2 = 3041

    if @a is null select '@A is null'

    if (@A <> @b-2) select 'NULL is not equal to 3041'

    else select 'NULL is equal to 3041'

    I know this will work if I set @a to zero before the '(@A <> @b-2)' check.

    But,why should NULL here be equal to a number?

  • Sorry, this is the wrong forum for this question, but still.. any answers???

  • It depends on your setting for ANSI_NULLS

    is you set this to off

    SET ANSI_NULLS off

    then your code will return 'NULL is equal to 3041'

    check out this microsoft link for a full explanation.

    http://msdn.microsoft.com/en-us/library/aa259229.aspx

    This also highlights one of the reasons that it is considerd good practice to have comparisons use '=' to resolve to true, rather than using '<>'

    if (@A= @b-2) select 'NULL is equal to 3041'

    else select 'NULL is not equal to 3041'

  • if (@A <> @b-2) select 'NULL is not equal to 3041'

    else select 'NULL is equal to 3041'

    @a is not equal to the number in @b-2

    If ( @a <> @b-2 ) results in null so does NOT give select 'NULL is not equal to 3041'

    the only alternative you give is to output

    'NULL is equal to 3041'

    Nothing wrong with SQL here, your logic is a touch flawed

  • Maybe I should say it this way.

    Good practice is to always check for nulls

    so

    If ( @a <> @b-2 ) Print 'Not Equal'

    Else If (@A =@B ) Print 'Equal'

    Else print '@A or @b-2 is null'

    End

    End

  • Thank you!

Viewing 6 posts - 1 through 5 (of 5 total)

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