July 27, 2015 at 11:08 pm
Good question & new piece of information for me, so thanx a lot.
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
July 28, 2015 at 1:07 am
This was removed by the editor as SPAM
July 28, 2015 at 2:55 am
Also a good illustration of why NOT IN can be very risky. Using NOT EXISTS instead gives the result that would probably be intended in this scenario:
SELECT Value
FROM #tmpNewValue new
WHERE NOT EXISTS (
SELECT 1 FROM #tmpOldValue old
WHERE old.Value = new.Value);
July 28, 2015 at 5:40 am
Thanks for the question.
Good day to all.
---------------
Mel. 😎
July 28, 2015 at 5:40 am
Agree.
Richard Warr (7/28/2015)
Also a good illustration of why NOT IN can be very risky. Using NOT EXISTS instead gives the result that would probably be intended in this scenario:
SELECT Value
FROM #tmpNewValue new
WHERE NOT EXISTS (
SELECT 1 FROM #tmpOldValue old
WHERE old.Value = new.Value);
---------------
Mel. 😎
July 28, 2015 at 6:40 am
July 28, 2015 at 6:43 am
This behavior is a lot easier to understand if you think about NOT IN as a series of 'not equal' statements logically ANDed together:
SELECT Value
FROM #tmpNewValue
WHERE Value != 3
AND Value != 7
AND Value != Null
AND Value != 6
Since any evaluation with Null is undetermined (which gets logically interpreted as false), the whole expression evaluates to false and no rows are returned.
July 28, 2015 at 6:55 am
Alternative solutions:
SELECT Value
FROM #tmpNewValue
WHERE Value not in(
SELECT Value FROM #tmpOldValue where Value is not null
)
SELECT Value
FROM #tmpNewValue
WHERE Value <> all(
SELECT Value FROM #tmpOldValue where Value is not null
)
Gerald Britton, Pluralsight courses
July 28, 2015 at 7:04 am
NULL can cause grief if you're not careful.
July 28, 2015 at 9:07 am
If the question could have been asked to check the returned value:
SELECT Value
FROM #tmpOldValue
WHERE Value NOT IN (SELECT Value FROM #tmpNewValue)
The answer would have been := 6
Thanks.
July 28, 2015 at 9:28 am
SQL-DBA-01 (7/28/2015)
If the question could have been asked to check the returned value:
SELECT Value
FROM #tmpOldValue
WHERE Value NOT IN (SELECT Value FROM #tmpNewValue)
The answer would have been := 6
Please, lets not start discussing all the possible answers if the question was different.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
July 28, 2015 at 10:29 am
Easy... Thanks, Sergey!
July 28, 2015 at 10:35 am
This was a good question that makes one really think about null value.
July 28, 2015 at 1:36 pm
sestell1 (7/28/2015)
This behavior is a lot easier to understand if you think about NOT IN as a series of 'not equal' statements logically ANDed together:
Perfect way to restate the original point.
Thanks,
Naveen.
Every thought is a cause and every condition an effect
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply