SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 combining sets, NULLs
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLS33ker
Starting Member

USA
12 Posts

Posted - 07/24/2013 :  01:03:52  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 07/24/2013 :  01:47:44  Show Profile  Reply with Quote
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

Edited by - visakh16 on 07/24/2013 01:50:31
Go to Top of Page

SQLS33ker
Starting Member

USA
12 Posts

Posted - 07/24/2013 :  01:51:48  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 07/24/2013 :  02:26:31  Show Profile  Reply with Quote
welcome

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

madhivanan
Premature Yak Congratulator

India
22713 Posts

Posted - 07/28/2013 :  12:17:34  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000