Friday, March 23, 2012

Mulitple Joins and Nulls

I am trying to join two tables on multiple fields. But the nulls aren't considered a match so they aren't included in the results set.

Select A.Lot, A.Block, A.Plan, B.Key

from A join B on

A.Lot=B.Lot

A.Block=B.Block

A.Plan=B.Plan

In the data, there can be an instance where Block is null in both tables so it "matches" but not in SQL. How do I get the "matched" nulls to be returned as well?

You can't match on the nulls in the database.|||Try something like this:
Select A.Lot, A.Block, A.Plan, B.KeyFrom Ajoin Bon A.Lot = B.LotAnd A.Plan = B.PlanAnd( (A.BlockIsNull And B.BlockIsNull)Or (A.Block = B.Block))
|||Richard, that worked great. Thanks for the excellent advice.

No comments:

Post a Comment