Join on Column A when Column B is Null

  • hi,

    I need to join on a table T1 AND Table T2.

    Something like this :

    Select * from Table T1

    JOIN TableT2 on T1.A =T2.A

    but here , condition is if  (T1.a or t2.a is null ) then join on (T1.b =t2.b ) where t1.b and t2.b is not null.

     

    How to achieve this?

     

  • Select * from Table T1

    JOIN TableT2 on (T1.A IS NOT NULL AND T1.A =T2.A) OR (T1.A IS NULL AND T1.B = T2.B)

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Presumably, you've tried this and it did not work out the way you wanted?

    SELECT *
    FROM T1
    JOIN T2
    ON (
    T1.A = T2.A
    OR T1.B = T2.B
    );

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Thanks....This join works for me.....as i am looking for both results either join on t1.a =t2.a or t1.b=t2.b.....this returns results , when either of the fields ( t1.a or t1.b) is null.

     

     

    • This reply was modified 1 year, 4 months ago by  komal145.
    • This reply was modified 1 year, 4 months ago by  komal145.
    • This reply was modified 1 year, 4 months ago by  komal145.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply