ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

COALESCE doesn't always work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • COALESCE doesn't always work

    Coalesce is handy. The definition of it is "The COALESCE() function returns the first non-null value in a list.". However, it doesn't seem to work for certain scenarios... Why don't these two examples work?

    Code:
    select coalesce(1/0,0) from SYSIBM.SYSDUMMY1
    
    select COALESCE(DATE('2020-02-31'),'0001-01-01') from SYSIBM.SYSDUMMY1
    In the first statement above, it's dividing by zero which is invalid, but I would have thought it would then give me the zero. In the second one, I would have thought the invalid date of Feb 31 would have caused it to give me the second date. Both of the above statements give me null instead of my non-null alternative.

    I thought I even saw an IBM example of coalesce being used on dates.

  • #2
    Built-in operations/functions (like mathematical calculations or DATE() ) don't actually return null if they are invalid, but instead some kind of soft error. In both cases those returned an SQL Warning status code (01564 Data conversion or data mapping error and 01534 Value in date, time, or timestamp string not valid respectively). In that situation I think the SQL processor does not return a value for those columns, which in a select view looks like null, but is not actually null

    Comment

    Working...
    X