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.
| Author |
Topic |
|
figmo
Starting Member
18 Posts |
Posted - 2009-03-01 : 15:33:40
|
| This works as expected for me:SELECT t1.id, t1.col, t2.col FROM table1 AS t1FULL OUTER JOIN table2 as t2 on t1.id=t2.idWHERE t1.col = t2.colIt returns rows from both tables with matching 'col' data.This does not work:SELECT t1.id, t1.col, t2.col FROM table1 AS t1FULL OUTER JOIN table2 as t2 on t1.id=t2.idWHERE t1.col <> t2.colAlways returns zero rows. Can sombody explain why? There are rows in both tables that have matching cols, and rows in both tables that do not. When put a where clause in for matching columns it works. If I change only the where clause to try to find cols that do not match, it returns an empty result set.NOTE: I have experimented a little with this. And I tried using the exact same syntax but with a different col. The one listed above giving me trouble is of type nvarchar. I tried the same script using a different column - of type smalldatetime - and it appeared to worked both ways. I could have a where clause of <> and saw only those rows where the smalldatetime col was not equal. So I suspect my syntax is ok. Is there something special I need to do about nvarchar in this context? |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-03-01 : 17:12:47
|
| Are you looking for this one? See SQLfor Girl's Answer.http://www.sqlteam.com/FORUMS/topic.asp?TOPIC_ID=120164 |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2009-03-01 : 18:11:50
|
figmo, I don't believe this has to do with nvarchar. The only explanation I can think of is that there are no rows where id in table1 equals the id in table2 and col in table1 does not equal col in table2.In SQL's 3-valued logic, null does not equal null (in most cases), so if that is what you are depending on, follow sodeep's advice posted above.Here is an example which gives data for both of your queries.declare @t1 table (id int, col varchar(32));declare @t2 table (id int, col nvarchar(32));insert into @t1 (id, col) values (1,'1');insert into @t1 (id, col) values (1,'10');insert into @t2 (id, col) values (1,N'1');insert into @t2 (id, col) values (2,N'10');SELECT t1.id, t1.col, t2.id, t2.col FROM @t1 t1FULL OUTER JOIN @t2 t2 on t1.id=t2.idWHERE t1.col = t2.colSELECT t1.id, t1.col, t2.id, t2.col FROM @t1 t1FULL OUTER JOIN @t2 t2 on t1.id=t2.idWHERE t1.col <> t2.col |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-02 : 10:33:41
|
quote: Originally posted by figmo This works as expected for me:SELECT t1.id, t1.col, t2.col FROM table1 AS t1FULL OUTER JOIN table2 as t2 on t1.id=t2.idWHERE t1.col = t2.colIt returns rows from both tables with matching 'col' data.This does not work:SELECT t1.id, t1.col, t2.col FROM table1 AS t1FULL OUTER JOIN table2 as t2 on t1.id=t2.idWHERE t1.col <> t2.colAlways returns zero rows. Can sombody explain why? There are rows in both tables that have matching cols, and rows in both tables that do not. When put a where clause in for matching columns it works. If I change only the where clause to try to find cols that do not match, it returns an empty result set.NOTE: I have experimented a little with this. And I tried using the exact same syntax but with a different col. The one listed above giving me trouble is of type nvarchar. I tried the same script using a different column - of type smalldatetime - and it appeared to worked both ways. I could have a where clause of <> and saw only those rows where the smalldatetime col was not equal. So I suspect my syntax is ok. Is there something special I need to do about nvarchar in this context?
nothing special. the reason is this , making where condition <> wont give you anything as for unmatched ones, the value of col from one of tables will be null. operators like =,<>,>,... will not work with null , because null is not stored as a value, unless you've set ansi null settings as off. so either turn it off to consider null as a value or use something like SELECT t1.id, t1.col, t2.col FROM table1 AS t1FULL OUTER JOIN table2 as t2 on t1.id=t2.idWHERE COALESCE(t1.col,'') <> COALESCE(t2.col,'') to turn NULLs to valid value |
 |
|
|
figmo
Starting Member
18 Posts |
Posted - 2009-03-04 : 13:53:24
|
| Sorry for the delay in responding. I've been down for 2 days with a nasty flu. Feeling better now and ready to get back at it.That is exactly what my problem was. "null does not equal null". The COALESCE() function was what I was missing.Thank you all for the help. |
 |
|
|
|
|
|
|
|