SQL comparison "ANSI_NULLS OFF"

I want to check if @a is different from @b in "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 is pretty 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 to do this? Thanks, Nestor

+3
source share
1 answer

following your logic, not using ISNULL or COALESCE, try the following:

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, but:

select
  case 
     when @a=@b OR COALESCE(@a,@b) is null then 'equal'
     else 'diff'
  end
+1
source

Source: https://habr.com/ru/post/1718943/


All Articles