tags:

views:

55

answers:

1

I want to check if @a is different from @b in the "ansi_nulls off":

set ansi_nulls off
declare @a int = 1;
declare @b int = null;
select case when @a<>@b then 'diff' else 'equal' end '@a=@b ?' --RETURNS 'diff'

but without using "set ansi_nulls off". I came up with the following, but it's quite verbose:

select
  case 
     when @a is null       and    @b is not null    then 'diff' -- null x
     when @a is not null   and    @b is null        then 'diff' -- x null
     when @a is null       and    @b is null        then 'equal' -- null null
     when @a <> @b                                  then 'diff' -- x x
     else 'equal'
  end

is there a shorter way of doing this? Thanks, Nestor

A: 

sticking to your logic, and not using ISNULL or COALESCE, try this:

select
  case 
     when @a=@b                      then 'equal' -- x x
     when @a is null and @b is null  then 'equal' -- null null
     else 'diff'
  end

this is better though:

select
  case 
     when @a=@b OR COALESCE(@a,@b) is null then 'equal'
     else 'diff'
  end
KM
Thanks. Sometimes I think SQL should have a special operator for non ansi comparisons. Right? Something like @a=?=@b, @a<?>@b ...
Nestor