Search code examples
sqloracle-databasesql-updatecoalesce

Oracle COALESCE or NVL


I have a stored procedure and I want to update a value in a table with the SYSDATE only if a parameter is NOT NULL.

In the following SQL, I want to set SYSENDDATE to NULL if pETime IS NULL Otherwise to SYSDATE

UPDATE OLCACT SET 
    ENDDATE    = pETime, 
    SYSENDDATE = SYSDATE, 
    GRD        = pGRD,
    PASS       = v_pass

Not sure how to use either NVL or COALESCE to do that.


Solution

  • nvl2 (yeah, "great" name, I know) will actually be much more convenient:

    UPDATE OLCACT SET 
        ENDDATE    = pETime, 
        SYSENDDATE = NVL2(pETime, SYSDATE, NULL)
        GRD        = pGRD,
        PASS       = v_pass