SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Nulls


Nulls

Author
Message
psingla
psingla
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1374 Visits: 1249
Comments posted to this topic are about the item Nulls

Pramod
SQL Server DBA | MCSE SQL Server 2012/2014

in.linkedin.com/in/pramodsingla/
http://pramodsingla.wordpress.com/
jaganmohan.rao
jaganmohan.rao
SSC Eights!
SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)SSC Eights! (868 reputation)

Group: General Forum Members
Points: 868 Visits: 71
Good question! But missed the Batch Separator(GO(default) or Wink 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.
tommyh
tommyh
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2522 Visits: 2000
jaganmohan.rao (12/7/2010)
Good question! But missed the Batch Separator(GO(default) or Wink 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
savosin_sergey
savosin_sergey
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 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"
honza.mf
honza.mf
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2539 Visits: 1323
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
vk-kirov
vk-kirov
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4330 Visits: 4408
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.
kent_secher
kent_secher
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 183
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.
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18625 Visits: 12426
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
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61367 Visits: 13297
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?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
ziangij
ziangij
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3588 Visits: 377
mistakenly clicked on the NULLS answer... Sad

nice question, thanks.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search