Home All Groups Group Topic Archive Search About

ANSI_NULL off with JOIN

Author
24 Aug 2006 11:27 AM
tjibbechris
I'm a bit confused about the ANSI_NULL setting in SQL 2000 (sp3).
Given the following script:

-- Start script
SET ANSI_NULLS OFF

SELECT
a.c AS a
,b.c AS b
FROM       (SELECT NULL c ) a
INNER JOIN (SELECT NULL c ) b ON b.c = a.c
-- End script

I would expect this to return one row with two columns, both containing
the value NULL (because NULL=NULL should yield true with ANSI_NULLS set
to off). Appearantly it does not. Does anyone know how I could achieve
this result?

tc

Author
24 Aug 2006 11:42 AM
David Portas
tjibbech***@gmail.com wrote:
Show quote
> I'm a bit confused about the ANSI_NULL setting in SQL 2000 (sp3).
> Given the following script:
>
> -- Start script
> SET ANSI_NULLS OFF
>
> SELECT
>  a.c AS a
>  ,b.c AS b
> FROM       (SELECT NULL c ) a
> INNER JOIN (SELECT NULL c ) b ON b.c = a.c
> -- End script
>
> I would expect this to return one row with two columns, both containing
> the value NULL (because NULL=NULL should yield true with ANSI_NULLS set
> to off). Appearantly it does not. Does anyone know how I could achieve
> this result?
>
> tc

Leave ANSI_NULLS permanently ON unless you require compatibility with
legacy code. Nulls cause enough trouble even without the extra
non-standards quirks.

SELECT
a.c AS a
,b.c AS b
FROM       (SELECT NULL c ) a
INNER JOIN (SELECT NULL c ) b
ON COALESCE(b.c,'foobar') = COALESCE(a.c,'foobar');

This begs the question, why would it make logical sense to join nulls
in that way? With a better design it ought to be possible to avoid
doing so.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Author
24 Aug 2006 12:38 PM
Scott Morris
> I would expect this to return one row with two columns, both containing
> the value NULL (because NULL=NULL should yield true with ANSI_NULLS set
> to off). Appearantly it does not. Does anyone know how I could achieve
> this result?

No - BOL is quite clear on the topic.  You made a bunch of extrapolations
and assumptions.  BOL says a comparison in the where clause involving a
column comparison with the NULL keyword will succeed.  It doesn't address
column to column comparison at all.  Below is the rather bizarre syntax you
would need to use - I can't think of any good reason to use it.

SET ANSI_NULLS OFF

SELECT
a.c AS a
,b.c AS b
FROM       (SELECT NULL c ) a
cross JOIN (SELECT NULL c ) b
where b.c = null
and a.c = null

SELECT a.*, b.*
FROM (SELECT NULL c ) a,
(SELECT NULL c ) b
where b.c = null
and a.c = null

AddThis Social Bookmark Button