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 12345»»»

Nulls Expand / Collapse
Author
Message
Posted Tuesday, December 7, 2010 8:56 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, July 4, 2014 1:16 AM
Points: 608, Visits: 1,024
Comments posted to this topic are about the item Nulls

Pramod
SQL Server DBA | MCSA SQL Server 2012

in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
Post #1031625
Posted Tuesday, December 7, 2010 9:58 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, March 25, 2013 4:03 AM
Points: 850, Visits: 71
Good question! But missed the Batch Separator(GO(default) or ;) between the Insert statements.

INSERT INTO dbo.Employees(EmployeeID) values (1)
GO
INSERT INTO dbo.Employees(EmployeeID) values (2)
GO
INSERT INTO dbo.Employees(EmployeeID) values (3)
GO
INSERT INTO dbo.Employees(EmployeeID) values (4)
GO


Regards,
Jagan.
Post #1031635
Posted Tuesday, December 7, 2010 11:31 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:26 PM
Points: 1,481, Visits: 1,960
jaganmohan.rao (12/7/2010)
Good question! But missed the Batch Separator(GO(default) or ;) between the Insert statements.

INSERT INTO dbo.Employees(EmployeeID) values (1)
GO
INSERT INTO dbo.Employees(EmployeeID) values (2)
GO
INSERT INTO dbo.Employees(EmployeeID) values (3)
GO
INSERT INTO dbo.Employees(EmployeeID) values (4)
GO


Which really arnt needed. None of them are (atleast not if your using SSMS).

But i also liked the question. Since it wasnt that long ago i ran into this somewhere on this site (QOD/Post/Article... something). And refreshing that now makes it much more likely that i will actually remember it

/T
Post #1031663
Posted Tuesday, December 7, 2010 11:32 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, July 4, 2014 12:41 AM
Points: 77, Visits: 37
Yes, question is instrustive (for me), but BOL says:
Caution:
Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results

So, results may be different! By the way, using Set ansi_nulls off, the result is three rows: "2, 3, 4"
Post #1031664
Posted Wednesday, December 8, 2010 12:07 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 6:20 AM
Points: 1,330, Visits: 1,312
savosin_sergey (12/7/2010)
Yes, question is instrustive (for me), but BOL says:
Caution:
Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results

So, results may be different! By the way, using Set ansi_nulls off, the result is three rows: "2, 3, 4"

ANSI_NULLS settings are discussed every time null values are mentioned. If someone wants to make it a point of the question, they can add the "It depands" answer.
Null values in negative comparisons are allways dangerous.




See, understand, learn, try, use efficient
© Dr.Plch
Post #1031673
Posted Wednesday, December 8, 2010 1:20 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Good question.

But this is not an explanation at all:
Explanation: If a subquery returns any null value the main query will not return any rows


I suggest the following explanation.

The SELECT query can be modified as follows:
select *
from dbo.Employees
where EmployeeID not in (SELECT NullableColumn FROM NullOperation)

-->
select *
from dbo.Employees
where EmployeeID not in (NULL, 0, 1)

-->
select *
from dbo.Employees
where (EmployeeID <> NULL) and (EmployeeID <> 0) and (EmployeeID <> 1)

The result of the comparison "EmployeeID <> NULL" is undefined for all values of EmployeeID (unless you set the ANSI_NULLS option to OFF). So the result of the WHERE clause is undefined (i.e. not true), and no rows are returned.
Post #1031690
Posted Wednesday, December 8, 2010 1:26 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 5:07 AM
Points: 268, Visits: 179
Interesting question. But at the least I will have to question the explanation ("If a subquery returns any null value the main query will not return any rows"). If you change the query to:

select * from dbo.Employees 
where EmployeeID in (SELECT NullableColumn FROM NullOperation)

Then a single row is returned (1), even though the subquery still returns a null value. So I think a better explanation is that the IN operator returns NULL if the value is not found in the list, and the list contains NULL values.
Post #1031691
Posted Wednesday, December 8, 2010 2:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:53 PM
Points: 5,890, Visits: 8,148
Good question. But the explanation is not an explanation at all. I wrote a series of blog posts about NULLs, and one part covers (a.o.) this issue - you can find it at http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/22/dr-unknown-or-how-i-learned-to-stop-worrying-and-love-the-null.aspx. (I recommend reading the other three parts as well - links to parts 1 and 2 are in the opening paragraph, and a link to part 4 is in the comments).


savosin_sergey (12/7/2010)
Yes, question is instrustive (for me), but BOL says:
Caution:
Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results

So, results may be different! By the way, using Set ansi_nulls off, the result is three rows: "2, 3, 4"

Results are unexpected, not undefined or unpredictable. Unexpected means that the results may not match human expectation, not that results may vary.
ANSI_NULLS is on by default, and ANSI_NULLS OFF is deprecated, so I think it's fair to assume ANSI_NULLS on unless explicitly told otherwise - except, maybe, when the author includes "it depends" in the answer options.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1031701
Posted Wednesday, December 8, 2010 2:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:18 AM
Points: 13,185, Visits: 10,040
I like the point the question tries to make (which is a very important one), but the explanation is lacking. Some links would have been nice and an explanation of ANSI_NULLS would have been great too (even if it is set to ON by default and the OFF is deprecated. Some people are still working with earlier versions of SQL Server.)



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1031710
Posted Wednesday, December 8, 2010 3:22 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 30, 2014 2:51 AM
Points: 1,956, Visits: 369
mistakenly clicked on the NULLS answer...

nice question, thanks.
Post #1031745
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse