Handling Null in Greatest function in Oracle

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

    Another version using a case expression to handle the null values:

    select cola, colb, 
      case when cola is null and colb is null then null
        when cola is null then colb
        when colb is null then cola
        else greatest(cola, colb)
      end as output
    from ;
    
    COLA       COLB       OUTPUT   
    ---------- ---------- ----------
    
    09/21/2013 01/02/2012 09/21/2013 
               01/03/2013 01/03/2013 
    01/03/2013            01/03/2013 
    

    提交回复
    热议问题