sql - Finding max of nullable date between two columns in db2 -


i have table in db2 called mytable.

it has several columns:

a |  b    | date1        |  date2 --------------------------------------------- 1    abc     <null>        2014-09-02 2    aax     2015-12-30    2016-09-02 2    bax     2015-10-20    <null> 2    ayx     2014-12-10    2016-02-12 

as seen values above, date1 , date2 can have null values well.

how can max of both date1 , date2 ?

i.e. output of query should 2016-09-02 max date of dates present in date1 , date2.

i using db2-9.

thanks reading!

how using union query:

select max(t.newdate) (     select date1 newdate     mytable     union     select date2 newdate     mytable ) t 

another option:

select case when t.date1 > t.date2 t.date1 else t.date2 end (     select (select max(date1) mytable) date1,            (select max(date2) mytable) date2     sysibm.sysdummy1 ) t 

Comments