title: MySQL: get MAX or GREATEST of several columns, but with NULL fields
body: <p>I'm trying to select the max date in three different fields in each record (MySQL)
  So, in each row, I have date1, date2 and date3: date1 is always filled, date2 and date3 can be NULL or empty
  The GREATEST statement is simple and concise but has no effects on NULL fields, so this doesn't work well:</p>
  
  <pre><code>SELECT id, GREATEST(date1, date2, date3) as datemax FROM mytable
  </code></pre>
  
  <p>I tried also more complex solutions like this:</p>
  
  <pre><code>SELECT
      CASE
          WHEN date1 &gt;= date2 AND date1 &gt;= date3 THEN date1
          WHEN date2 &gt;= date1 AND date2 &gt;= date3 THEN date2
          WHEN date3 &gt;= date1 AND date3 &gt;= date2 THEN date3
          ELSE                                        date1
      END AS MostRecentDate
  </code></pre>
  
  <p>Same problem here: NULL values are a GREAT problem in returning the right records</p>
  
  <p>Please, have you got a solution?
  Thanks in advance....</p>