title: Find the minimum date, but not null from different columns in a select
body: <p>I have a Teradata SQL query with several different date fields which can be NULL.
  I want to find the earliest/lowest date, and return NULL only if all dates are NULL. </p>
  
  <p>I tried the function LEAST, but that returns NULL as long as any date is NULL, as it is considered the lowest by this function.</p>
  
  <p>My quick fix now is to COALESCE the dates to '9999-12-31' and then use a very ugly CASE statement to replace '9999-12-31' with NULL if that is the result of the LEAST function.</p>
  
  <p>A minimal example of my code now:</p>
  
  <pre><code>CASE WHEN 
  LEAST(
  COALESCE(date1, date '9999-12-31')(INTEGER),
  COALESCE(date2, date '9999-12-31')(INTEGER)
  ) = '9999-12-31' THEN NULL
  ELSE
  LEAST(
  COALESCE(date1, date '9999-12-31')(INTEGER),
  COALESCE(date2, date '9999-12-31')(INTEGER)
  )(DATE) 
  END
  </code></pre>
  
  <p>I would like it to be something like:</p>
  
  <pre><code>LEAST_WITHOUT_NULL(date1,date2)
  </code></pre>
  
  <p>And have it return the earliest date and NULL only in the case both dates are NULL.</p>
  
  <p>I hope someone can suggest a better function, or even some better way to rewrite this.</p>