The Subtle Differences Between COALESCE and ISNULL

  • Comments posted to this topic are about the item The Subtle Differences Between COALESCE and ISNULL

  • This was removed by the editor as SPAM

  • Would there be reasons to not just use COALESCE all the time? For the simple case, the effort to use COALESCE instead of ISNULL is the same.

  • One more for your list.

    The result of an ISNULL() is NOT NULL, but the result of a coalesce is NULLable.

    EXEC sp_describe_first_result_set N'SELECT OrderID, ISNULL(Discount, 0) AS DiscountValueIsNull, COALESCE(Discount, 0) AS DiscountValueCoalesce
    FROM Orders;'
  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

  • I have found no reason to use ISNULL over COALESCE. I believe ISNULL is specific to SQL Server while COALESCE is specified in the ISO/IEC standard.

    I thought this article did nice job of telling us the difference but a not so good job of explaining the difference. I didn't see one example where the 2nd value in the chain was actually used to show a difference in outcome.

    ewm2

  • edmil wrote:

    I have found no reason to use ISNULL over COALESCE. I believe ISNULL is specific to SQL Server while COALESCE is specified in the ISO/IEC standard.

    I'm just the opposite.  I prefer ISNULL because I've never had a need for the depth of coalesce.  I learned years (read: decades) ago that the optimizer is replacing "ISNULL(Discount, 0.00)" with "case when Discount is null then 0 else Discount end".  I'm not sure if the optimizer is doing that with coalesce, so I've always leaned on ISNULL.  My mind likes that comfort knowing that nothing fancy is happening (also, I'll never work on anything outside of SQL Server, so standards-be-damned).  😛

Viewing 8 posts - 1 through 7 (of 7 total)

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