Greatest not null column

Posted by Álvaro G. Vicario on Stack Overflow See other posts from Stack Overflow or by Álvaro G. Vicario
Published on 2010-04-21T15:01:14Z Indexed on 2010/04/21 15:03 UTC
Read the original article Hit count: 142

Filed under:
|
|

I need to update a row with a formula based on the largest value of two DATETIME columns. I would normally do this:

GREATEST(date_one, date_two)

However, both columns are allowed to be NULL. I need the greatest date even when the other is NULL (of course, I expect NULL when both are NULL) and GREATEST() returns NULL when one of the columns is NULL.

This seems to work:

GREATEST(COALESCE(date_one, date_two), COALESCE(date_two, date_one))

But I wonder... am I missing a more straightforward method?

© Stack Overflow or respective owner

Related posts about sql

Related posts about Oracle