COALESCE in Left Join

  • Comments posted to this topic are about the item COALESCE in Left Join

    Thanks,
    Naveen.
    Every thought is a cause and every condition an effect

  • Good brain training!

    Thanks!

    😀

  • Only 54% correct? I was expecting close to 100%!

  • Great question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Toreador (4/29/2015)


    Only 54% correct? I was expecting close to 100%!

    It depends if one has enough time to read correctly the question of th day.

  • The question assumes SET ANSI_NULLS ON Although it is ON by default, and will stay ON on later SQL version it's not always the case on older SQL Server versions.

  • Well I tried it and got 7 and 7.

    create table #abc (a int, b int, c int);

    INSERT INTO #abc values

    (NULL,NULL,3),

    (1,NULL,3),

    (1,NULL,NULL),

    (NULL,2,NULL);

    Create table #xyz (x int, y int, z int);

    INSERT INTO #abc values

    (NULL,2,3),

    (NULL,NULL,1),

    (NULL,NULL,2);

    SELECT

    *

    FROM

    #abc a

    LEFT JOIN #xyz x

    ON a.a = x.x

    AND a.b = x.y;

    -- Q2

    SELECT

    *

    FROM

    #abc a

    LEFT JOIN #xyz x

    ON COALESCE(a.a, 9) = COALESCE(x.x, 9)

    AND COALESCE(a.b, 9) = COALESCE(x.y, 9);

    drop table #abc;

    drop table #xyz;

  • Probably because you've got a typo:

    Create table #xyz (x int, y int, z int);

    INSERT INTO #abc values

    (NULL,2,3),

    (NULL,NULL,1),

    (NULL,NULL,2);

  • Thanks! Copy paste will get you in trouble.:-D

  • Good question to start the day. An exercise in clear thinking is always good for the brain. Thanks.

  • Thanks. Good training on COALESCE which I rarely use.

  • How about use ISNULL?

    -- Q3

    SELECT

    *

    FROM

    #abc a

    LEFT JOIN #xyz x

    ON ISNULL(a.a, 9) = ISNULL(x.x, 9)

    AND ISNULL(a.b, 9) = ISNULL(x.y, 9);

    --------------------------------------
    ;-)“Everything has beauty, but not everyone sees it.” ― Confucius

  • chgn01 (4/29/2015)


    How about use ISNULL?

    -- Q3

    SELECT

    *

    FROM

    #abc a

    LEFT JOIN #xyz x

    ON ISNULL(a.a, 9) = ISNULL(x.x, 9)

    AND ISNULL(a.b, 9) = ISNULL(x.y, 9);

    In this case, they'll act the same way. But that won't be always the case.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks for this interesting question!

  • Luis Cazares (4/29/2015)


    chgn01 (4/29/2015)


    How about use ISNULL?

    -- Q3

    SELECT

    *

    FROM

    #abc a

    LEFT JOIN #xyz x

    ON ISNULL(a.a, 9) = ISNULL(x.x, 9)

    AND ISNULL(a.b, 9) = ISNULL(x.y, 9);

    In this case, they'll act the same way. But that won't be always the case.

    If there's no coercing data going on, it should be the same. There's probably some esoteric exception though.

    Don Simpson



    I'm not sure about Heisenberg.

Viewing 15 posts - 1 through 15 (of 19 total)

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