Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to update particular column value to today's date for the particular ID and keeping other value

Albert ChaoFeb 2 2022
CREATE TABLE e_tab (
    e_id           NUMBER(10),
    eligible       VARCHAR2(30),
    assigned_date  TIMESTAMP
);

INSERT INTO e_tab VALUES(1,'Y',null);
INSERT INTO e_tab VALUES(2,'Y',null);
INSERT INTO e_tab VALUES(3,null,null);

SET SERVEROUTPUT ON;

DECLARE
    lv_flag NUMBER(10);
BEGIN
    SELECT
        COUNT(1)
    INTO lv_flag
    FROM
        e_tab
    WHERE
        assigned_date != sysdate;

    IF lv_flag < 1 THEN
        UPDATE e_tab
        SET
            assigned_date = current_timestamp
        WHERE
            eligible = 'Y';

    END IF;

    COMMIT;
END;

I have a table e_tab that I need to update based on the eligible column. If eligible column is Y then I need to update assigned_date column to current date and this will not happen frequently. Say for 2 E_ID that is 1 and 2 eligible column is Y so I have updated assigned_date column to today's date but for the third e_id i.e 3 that will be updated say after one day and will update eligible column to Y. Then, in this case, it should only update the third e_id to tomorrow's date and the previous one will be as it is.

Explanation example :
I need only those records to be updated when eligible a flag is changing to Y. Let's assume I am updating eligible column of e_id 3 on 3rd Feb 2022. Then in this case for e_id 1 and 2 that will show as 2nd Feb only but on 3rd Feb I have changed the flag to Y for e_id 3. So, only e_id 3 should have 3rd Feb and e_id 1 & 2 will have 2nd Feb only
Expected output:

+------+----------+-------------------------------+
| E_ID | ELIGIBLE |         ASSIGNED_DATE         |
+------+----------+-------------------------------+
|    1 | Y        | 02-02-22 3:53:46.449000000 PM |
|    2 | Y        | 02-02-22 3:53:46.449000000 PM |
|    3 | Y        | 03-02-22 3:53:46.449000000 AM |
+------+----------+-------------------------------+

Will my code work for this scenario? And also if a few more e_id got added later then the same should happen for all the e_id
I have also asked this question on stackoverflow.

This post has been answered by BluShadow on Feb 2 2022
Jump to Answer
Comments
Post Details
Added on Feb 2 2022
1 comment
16,979 views