Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««89101112

NULL Equals NULL? Expand / Collapse
Author
Message
Posted Friday, January 25, 2008 5:16 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 11:30 AM
Points: 354, Visits: 1,536
Whenever you compare NULL you should always use 'IS NULL' rather than '= NULL'
e.g.
SET ANSI_NULLS ON
DECLARE @val CHAR(4)
SET @val = NULL
SET ANSI_NULLS ON
If @val = NULL
PRINT 'TRUE'
ELSE IF NOT(@val = NULL)
PRINT 'FALSE'
ELSE
PRINT 'UNKNOWN'


Will return 'UNKNOWN'

but

SET ANSI_NULLS ON
DECLARE @val CHAR(4)
SET @val = NULL
SET ANSI_NULLS ON
If @val IS NULL
PRINT 'TRUE'
ELSE IF NOT(@val IS NULL)
PRINT 'FALSE'
ELSE
PRINT 'UNKNOWN'



Will return 'TRUE'

Post #447415
Posted Friday, January 25, 2008 4:28 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 11, 2015 3:06 PM
Points: 1,359, Visits: 7,212
this discussion was remarkable to read. i thought this article was interesting that i found by following some of the links cimode referenced...a lot of similarities to the "exchange" that occured here.

http://www.dbdebunk.com/page/page/861446.htm


bc
Post #447872
Posted Sunday, November 9, 2008 4:35 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, July 6, 2014 10:34 PM
Points: 17, Visits: 76
Hi,

I am having problems with my queries which I definitely think is because of null values. This is what I want to do:
Find out a list of phone numbers that are in one table but not in the other:
I wrote something like this:

select phonenumber

from ttemp

where phonenumber

not in

(

select ('7'+phonenumber)

from tpphone

where phonecountrycode = '64'

and phoneareacode = '07'

)

But it didnt return any records.
Now when I try the same as:

select phonenumber

from ttemp

where phonenumber

in

(

select ('7'+phonenumber)

from tpphone

where phonecountrycode = '64'

and phoneareacode = '07'

)

It returned some records.

The phonenumber column are permitted to have null values.

What could be the possible explanation for this behaviour
Post #599609
Posted Sunday, November 9, 2008 4:54 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 11:30 AM
Points: 354, Visits: 1,536
donnelcyril (11/9/2008)
Hi,

I am having problems with my queries which I definitely think is because of null values. This is what I want to do:
Find out a list of phone numbers that are in one table but not in the other:



If you have SQL Server 2005 or greater you can use 'EXCEPT':

SELECT phonenumber
FROM ttemp
EXCEPT
SELECT phonenumber
FROM tpphone
WHERE phonecountrycode = '64'
AND phoneareacode = '07'
Post #599612
Posted Sunday, November 9, 2008 5:02 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:31 PM
Points: 6,377, Visits: 8,726
donnelcyril (11/9/2008)
The phonenumber column are permitted to have null values.

What could be the possible explanation for this behaviour


Hi donnelcyril,

You already gave the reply - the null values are the explanation.

A NOT IN can only evaluate as True if none of the rows in the subquery match the value in the outer query. If one or more rows in the subquery has a NULL, then it is Unknown to the database whether that value is a match, and as such the entire NOT IN can result in False (if an equal value exists) or Unknown (if no known equal value exists - remember it's not known if the NULL values are equal or not).

To work around this (for some people very unexpected) behaviour, avoid NOT IN and use NOT EXISTS instead.

More information:
* What is null: http://sqlblog.com/blogs/hugo_kornelis/archive/2007/07/06/null-ndash-the-database-rsquo-s-black-hole.aspx
* Three-valued logic: http://sqlblog.com/blogs/hugo_kornelis/archive/2007/07/17/the-logic-of-three-valued-logic.aspx
* Unexpected consequences of null: http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/22/dr-unknown-or-how-i-learned-to-stop-worrying-and-love-the-null.aspx
* Functions and expressions for proper null handling: http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/30/what-if-null-if-null-is-null-null-null-is-null.aspx



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #599613
Posted Sunday, November 9, 2008 6:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, July 6, 2014 10:34 PM
Points: 17, Visits: 76
Hi,

That really explains why my query werent pulling out any records...
Well I was able to solve the problem by modifying it as below:

select phonenumber

from ttemp

where phonenumber

not in

(

select '7'+phonenumber

from tpphone

where phonecountrycode = '64'

and phoneareacode = '07'

and phonenumber is not null

)

However the query was taking a long time to execute and I had to pull my outer query into a temporary table to get things done.

Post #599618
Posted Wednesday, January 13, 2010 7:27 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, October 29, 2013 6:02 AM
Points: 147, Visits: 426
I haven't seen mention of this important NULL topic anywhere:

select 1
where 1 = 1 or datediff(hour, null, getdate()) > 4

TSQL seems to work like this:

where True OR NULL = True
where False OR NULL = False

where True AND NULL = False
where False AND NULL = False

which seems good to me

but I still think this syntax would be useful:

where coalesce(datediff(hour, [field], getdate()) > 4, 0)

letting you coalesce a boolean without using dummy values
Post #846880
Posted Monday, August 9, 2010 3:34 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 19, 2015 11:18 AM
Points: 1,277, Visits: 1,150
DataDog (1/13/2010)
I haven't seen mention of this important NULL topic anywhere:

select 1
where 1 = 1 or datediff(hour, null, getdate()) > 4

TSQL seems to work like this:

where True OR NULL = True
where False OR NULL = False

where True AND NULL = False
where False AND NULL = False

which seems good to me

but I still think this syntax would be useful:

where coalesce(datediff(hour, [field], getdate()) > 4, 0)

letting you coalesce a boolean without using dummy values


Actually, this:

datediff(hour, null, getdate()) > 4

Works out to this:

NULL > 4

Which returns 'UNKNOWN'. So your truth table entry for:

where 1 = 1 or datediff(hour, null, getdate()) > 4

Is actually:

True OR Unknown = True

That's covered fairly well in a lot of places--a lot of articles on this site, for instance or BOL.

Thanks
Mike C
Post #966281
« Prev Topic | Next Topic »

Add to briefcase «««89101112

Permissions Expand / Collapse