Handling Null in Greatest function in Oracle

后端 未结 9 1361
轮回少年
轮回少年 2021-02-14 05:36

I want to compare two dates from two columns and get the greatest and then compare against a date value.The two column can hold NULL values too.For example I want the below OUTP

9条回答
  •  暖寄归人
    2021-02-14 06:05

    You could remove the possibility of any of the columns returning NULL by using the NVL function. Substitute any NULL values with a date that is earlier than any date that is likely to occur in your tables.

    SELECT GREATEST(NVL(A,TO_DATE('01/01/1800','MM/DD/YYYY')), 
                    NVL(B,TO_DATE('01/01/1800','MM/DD/YYYY'))) AS OUTPUT
    FROM ...
    

    The GREATEST function will then return the most recent date (maximum date) from the list of supplied dates without inadvertently returning NULL if one or more of the columns contains NULL.

提交回复
热议问题