Re: nulls



On 21 Mar 2007 00:32:20 -0700, othellomy@xxxxxxxxx wrote:

(snip)
SET @a = ''
SET @b = ' '
if nullif(@a,'') is null and nullif(@b,'') is null
PRINT 'They are equal!';
ELSE
PRINT 'They are different!';

Hi othellomy,

I'm not sure what you're trying to say here. This code will return "They
are equal!" if both @a and @b are either NULL or a string consisting of
zero or more space characters, regardless of whether they are equal:

DECLARE @a varchar(10), @b varchar(10);
SET @a = ' ';
SET @b = NULL;

if nullif(@a,'') is null and nullif(@b,'') is null
PRINT 'They are equal!';

But it will return nothing if @a and @b are both non-NULL and not empty,
even if they ARE equal:

DECLARE @a varchar(10), @b varchar(10);
SET @a = 'X';
SET @b = @a;

if nullif(@a,'') is null and nullif(@b,'') is null
PRINT 'They are equal!';

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
.



Relevant Pages

  • Re: T-SQL Challenge
    ... However, for each Product and WE_DATE, we also want the demand units ... Hugo Kornelis, SQL Server MVP ... My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis ...
    (comp.databases.ms-sqlserver)
  • Re: reduce time for search query
    ... Posted by Peso: ... Hugo Kornelis, SQL Server MVP ... My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis ...
    (comp.databases.ms-sqlserver)
  • Re: Date Range for SQL 2005
    ... execute. ... Hugo Kornelis, SQL Server MVP ... My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis ...
    (comp.databases.ms-sqlserver)
  • Re: SQL Minus 7 days from current date
    ... Hugo Kornelis, SQL Server MVP ... My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis ...
    (microsoft.public.sqlserver.mseq)
  • Re: Create One Trigger For Both Update and Delete
    ... Hugo Kornelis, SQL Server MVP ... My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis ...
    (comp.databases.ms-sqlserver)