Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 combining sets, NULLs

Author  Topic 

SQLS33ker
Starting Member

16 Posts

Posted - 2013-07-24 : 01:03:52
Hi there,

When you join tables based on a common join in this syntax:

T1.col1 = T2.col1 OR (T1.col1 IS NULL and T2.col1 IS NULL);

What do texts within the bracket tell the server do? The textbook says we are saying to treat NULLs as equal. Does that mean all the null values will participate in the join? If null = null, then that's a match?

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-24 : 01:47:44
Yes..it just means when there's a NULL on both T1.Col1 and T2.Col1 then that records will matched against each other
NULL=NULL is never true
so to treat them as same we need to use either above method or
use ISNULL or COALESCE function to convert them to a common default value

like ISNULL(T1.col1,-1) = ISNULL(T2.col1,-1) if int
ISNULL(T1.col1,'') = ISNULL(T2.col1,'') if varchar etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

SQLS33ker
Starting Member

16 Posts

Posted - 2013-07-24 : 01:51:48
quote:
Originally posted by visakh16

Yes..it just means when there's a NULL on both T1.Col1 and T2.Col1 then that records will matched against each other
NULL=NULL is never true
so to treat them as same we need to use either above method or
use ISNULL or COALESCE function to convert them to a common default value

like ISNULL(T1.col1,-1) = ISNULL(T2.col1,-1) if int
ISNULL(T1.col1,'') = ISNULL(T2.col1,'') if varchar etc

great thanks.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-24 : 02:26:31
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-07-28 : 12:17:34
There is a post with examples at http://beyondrelational.com/modules/2/blogs/70/posts/10851/null-on-joined-columns.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -