replace null with another column

  • Brandie Tarvin (1/5/2017)


    Rasmus Remmer Bielidt (1/5/2017)


    Both options have their uses for sure.

    I didn't see anybody making this point, but COALESCE() absolutely needs a non-null argument or it will throw an error where a case without a "hit" will use the ELSE clause (or null where applicable).

    COALESCE(NULL,NULL)

    will get you

    Msg 4127, Level 16, State 1, Line 1

    At least one of the arguments to COALESCE must be an expression that is not the NULL constant.

    At least CASE let's you specify an ELSE for "none of the above".

    Indeed - as long you have the same number of ELSE as you have CASE 🙂

  • Rasmus Remmer Bielidt (1/5/2017)


    Brandie Tarvin (1/5/2017)


    Rasmus Remmer Bielidt (1/5/2017)


    Both options have their uses for sure.

    I didn't see anybody making this point, but COALESCE() absolutely needs a non-null argument or it will throw an error where a case without a "hit" will use the ELSE clause (or null where applicable).

    COALESCE(NULL,NULL)

    will get you

    Msg 4127, Level 16, State 1, Line 1

    At least one of the arguments to COALESCE must be an expression that is not the NULL constant.

    At least CASE let's you specify an ELSE for "none of the above".

    Indeed - as long you have the same number of ELSE as you have CASE 🙂

    <blink> Huh?

    I'm not sure I understand what you mean by that. Could you clarify?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • I've had to troubleshoot queries where the developer had forgotten an ELSE clause and the whole thing returned NULL (which was not expected).

    this will yield NULL due to not having an ELSE clause.

    SELECT CASE 1 WHEN 0 THEN 'NON-NULL' END AS "NONSENSE"

  • Rasmus Remmer Bielidt (1/5/2017)


    I've had to troubleshoot queries where the developer had forgotten an ELSE clause and the whole thing returned NULL (which was not expected).

    this will yield NULL due to not having an ELSE clause.

    SELECT CASE 1 WHEN 0 THEN 'NON-NULL' END AS "NONSENSE"

    Oh. Okay. The way you worded it, I first read it as "one ELSE per switch (WHEN) in the CASE statement". I agree that every CASE should have an ELSE. As should an IF.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Rasmus Remmer Bielidt (1/5/2017)


    Both options have their uses for sure.

    I didn't see anybody making this point, but COALESCE() absolutely needs a non-null argument or it will throw an error where a case without a "hit" will use the ELSE clause (or null where applicable).

    COALESCE(NULL,NULL)

    will get you

    Msg 4127, Level 16, State 1, Line 1

    At least one of the arguments to COALESCE must be an expression that is not the NULL constant.

    That's because it's nonsense to write COALESCE(NULL,NULL) or basically any other version of the COALESCE function using a NULL constant.

    The error won't appear when there's a NULL that comes from a column or variable. The reason is that it will have a type for the result of the function.

    The code you shared is basically this:

    SELECT CASE WHEN NULL IS NULL THEN NULL ELSE NULL END;

    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
  • Luis Cazares (1/5/2017)


    Rasmus Remmer Bielidt (1/5/2017)


    Both options have their uses for sure.

    I didn't see anybody making this point, but COALESCE() absolutely needs a non-null argument or it will throw an error where a case without a "hit" will use the ELSE clause (or null where applicable).

    COALESCE(NULL,NULL)

    will get you

    Msg 4127, Level 16, State 1, Line 1

    At least one of the arguments to COALESCE must be an expression that is not the NULL constant.

    That's because it's nonsense to write COALESCE(NULL,NULL) or basically any other version of the COALESCE function using a NULL constant.

    The error won't appear when there's a NULL that comes from a column or variable. The reason is that it will have a type for the result of the function.

    As Luis said, it's not because the arguments are NULL that's the problem, it's that it can't determine the data type. All of the following work, even though every single one of the arguments is null.

    DECLARE @i INT, @j-2 INT

    SELECT COALESCE(@i, @j-2), @i, @j-2

    SELECT COALESCE(@i, NULL)

    SELECT COALESCE(NULL, @j-2)

    SELECT COALESCE(CAST(NULL AS INT), NULL)

    The code you shared is basically this:

    SELECT CASE WHEN NULL IS NULL THEN NULL ELSE NULL END;

    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    ----------------------------------------------------

  • MMartin1 (1/9/2017)


    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    It's rather SELECT CASE WHEN NULL = NULL THEN NULL ELSE NULL END

    Fails the same way as COALESCE.

    Which proves - it's an adequate replacement.

    :hehe:

    _____________
    Code for TallyGenerator

  • And if you would use sub queries, would that be flexible enough for you? I've added an additional column to the example posted earlier to demonstrate what I mean. I've also added examples in the table values for the requested 'Dr.' + 'MD' combination. I haven't implemented all options though. It's just a demo.

    SELECT

    *,

    COALESCE(

    Title + ' ' + First_Name + ' ' + MI + ' ' + Last_Name + ', ' + Suffix,

    First_Name + ' ' + MI + ' ' + Last_Name + ', ' + Suffix,

    Title + ' ' + First_Name + ' ' + MI + ' ' + Last_Name,

    Title + ' ' + First_Name + ' ' + Last_Name + ', ' + Suffix,

    First_Name + ' ' + Last_Name + ', ' + Suffix,

    Title + ' ' + First_Name + ' ' + Last_Name,

    First_Name + ' ' + Last_Name,

    Title + ' ' + Last_Name

    ),

    LTRIM( COALESCE(Title, '')

    + COALESCE( ' ' + First_Name, '')

    + COALESCE( ' ' + MI, '')

    + COALESCE( ' ' + Last_Name, '')

    + COALESCE( ', ' + Suffix, '')

    ),

    (

    select top (1) t.txt

    from (

    select 1, x.Title + ' ' + x.First_Name + isnull(' ' + x.MI, '') + ' ' + x.Last_Name + ', ' + x.Suffix

    where x.Title is not null

    and x.First_Name is not null

    and x.Last_Name is not null

    and x.Suffix is not null

    and not (x.Suffix = 'MD' and x.Title = 'Dr.')

    union all

    select 2, x.Title + ' ' + x.First_Name + isnull(' ' + x.MI, '') + ' ' + x.Last_Name

    where x.Title is not null

    and x.First_Name is not null

    and x.Last_Name is not null

    and (x.Suffix is null or (x.Suffix = 'MD' and x.Title = 'Dr.'))

    -- union all

    -- etc...

    ) t (seqnr, txt)

    order by seqnr

    )

    FROM(

    VALUES ('Mr.', 'Luis', 'A', 'Cazares', 'Sr.'),

    (NULL, 'Luis', 'A', 'Cazares', 'Sr.'),

    (NULL, 'Luis', NULL, 'Cazares', 'Sr.'),

    (NULL, 'Luis', NULL, 'Cazares', NULL),

    ('Mr.', 'Luis', NULL, 'Cazares','Sr.'),

    ('Mr.', 'Luis', 'A', 'Cazares', NULL),

    ('Mr.', NULL, NULL, 'Cazares', NULL),

    ('Mr.', 'Luis', NULL, NULL, NULL),

    ('Mr.', NULL, NULL, 'Cazares', 'Sr.'),

    (NULL, NULL, 'A', 'Cazares', NULL),

    ('Dr.', 'Luis', NULL, 'Cazares','MD'),

    ('Dr.', 'Luis', NULL, 'Cazares','Sr.')

    ) x (Title, First_Name, MI, Last_Name, Suffix)

    You can even construct the union clauses and execute the resulting dynamic sql using sp_executesql to get some "programmability" in your rules.



    Posting Data Etiquette - Jeff Moden[/url]
    Posting Performance Based Questions - Gail Shaw[/url]
    Hidden RBAR - Jeff Moden[/url]
    Cross Tabs and Pivots - Jeff Moden[/url]
    Catch-all queries - Gail Shaw[/url]


    If you don't have time to do it right, when will you have time to do it over?

  • MMartin1 (1/9/2017)


    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    CASE cannot return a Boolean predicate, and since NOT is a Boolean operator, you cannot return NOT NULL in your THEN clause.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen (1/10/2017)


    MMartin1 (1/9/2017)


    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    CASE cannot return a Boolean predicate, and since NOT is a Boolean operator, you cannot return NOT NULL in your THEN clause.

    Drew

    Hence why I mentioned logically 🙂

    ----------------------------------------------------

  • MMartin1 (1/10/2017)


    drew.allen (1/10/2017)


    MMartin1 (1/9/2017)


    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    CASE cannot return a Boolean predicate, and since NOT is a Boolean operator, you cannot return NOT NULL in your THEN clause.

    Drew

    Hence why I mentioned logically 🙂

    Except it's not really logical to write code that will not work which will confuse future readers of this thread and actually wonder what they're doing wrong if they try to test the example you posted.

    And on that thought, we should start a "Things to try that won't work" thread to see how many people take it seriously. Who will be posting on April 1st? @=)

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Brandie Tarvin (1/10/2017)


    MMartin1 (1/10/2017)


    drew.allen (1/10/2017)


    MMartin1 (1/9/2017)


    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    CASE cannot return a Boolean predicate, and since NOT is a Boolean operator, you cannot return NOT NULL in your THEN clause.

    Drew

    Hence why I mentioned logically 🙂

    Except it's not really logical to write code that will not work which will confuse future readers of this thread and actually wonder what they're doing wrong if they try to test the example you posted.

    And on that thought, we should start a "Things to try that won't work" thread to see how many people take it seriously. Who will be posting on April 1st? @=)

    It is not really writing code as much as using the language in a pseudo fashion to make the logical statement. Anyone trying out the syntax will quickly realise that it throws an error.

    ----------------------------------------------------

  • MMartin1 (1/10/2017)


    Brandie Tarvin (1/10/2017)


    MMartin1 (1/10/2017)


    drew.allen (1/10/2017)


    MMartin1 (1/9/2017)


    You forgot the NOT.

    SELECT CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END;

    How about

    SELECT CASE WHEN NULL IS NOT NULL THEN NOT NULL ELSE NULL END

    ?

    same logical result to the original expression COALESCE(null, null) . Though if you keep going these nulls can start to become a head spinner.

    CASE cannot return a Boolean predicate, and since NOT is a Boolean operator, you cannot return NOT NULL in your THEN clause.

    Drew

    Hence why I mentioned logically 🙂

    Except it's not really logical to write code that will not work which will confuse future readers of this thread and actually wonder what they're doing wrong if they try to test the example you posted.

    And on that thought, we should start a "Things to try that won't work" thread to see how many people take it seriously. Who will be posting on April 1st? @=)

    It is not really writing code as much as using the language in a pseudo fashion to make the logical statement. Anyone trying out the syntax will quickly realise that it throws an error.

    Yeah... I can't see that. I would buy this argument more if it had been written more in a pseudo language fashion instead of using exact CASE statement syntax.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Hmm, which is why you have to read the context. If people are blindly pulling code they don't understand, then they are the ones choosing to take risk. If they don't err here, it will happen somewhere else. Still I take your point that is is more helpful to throw in a "don't try this at home" disclaimer.

    ----------------------------------------------------

Viewing 15 posts - 16 through 30 (of 32 total)

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