Home > Software design >  postgresql Select higher Timestamp with Null Values
postgresql Select higher Timestamp with Null Values

Time:09-14

The problem is if i compare two timestamps and one of them is NULL it chooses Null as higher Value.

SELECT case 
         when "Date1" < "Date2" then "Date1" 
         else "Date2"  
       end as "HigherDate"
FROM "table"

How can i select always the higher date between those two even if one of them is NULL?

CodePudding user response:

Use greatest() which is NULL safe:

select greatest(date1, date2) as higher_date
from the_table
  • Related