Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greatest not null column

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?

like image 994
Álvaro González Avatar asked Apr 21 '10 15:04

Álvaro González


People also ask

How do you handle the greatest NULL in Oracle?

Solution is simple: Add a nvl around each of your values and replace it with the value that suits your specific needs. Simple thing to remember: each function will return NULL when one of the parameters is NULL.

Does Max ignore nulls?

MAX ignores any null values. MAX returns NULL when there is no row to select. For character columns, MAX finds the highest value in the collating sequence.

Where columns are not NULL?

By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

What is a NULL column?

A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.


1 Answers

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

like image 184
Vadim K. Avatar answered Sep 21 '22 17:09

Vadim K.