Handling Null in Greatest function in Oracle

后端 未结 9 1365
轮回少年
轮回少年 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:01

    use NVL to solve this however complicity will be increased based of number of compared columns :

    select A.*, *greatest("COL A", "COL B") "DIRECT COMPARE"*, **greatest(nvl("COL A", "COL B"), nvl("COL B", "COL A")) "NVL COMPARE"**
    from (
    SELECT NULL "COL A", SYSDATE "COL B", SYSDATE "NEEDED RESULT" FROM DUAL UNION
    SELECT SYSDATE - 180 , NULL  , SYSDATE - 180 FROM DUAL UNION
    SELECT SYSDATE - 180 , SYSDATE , SYSDATE FROM DUAL ) A;
    

提交回复
热议问题