简体   繁体   中英

Update column based on year of another column in postgresql using a trigger

I am trying to create a trigger to update the column reminder(datatype date) using the data of the column date_of_join(datatype date) of same table. Reminder should be 1st of January, next year.

My function is :

CREATE OR REPLACE FUNCTION update_reminder()
RETURNS TRIGGER AS
$$
BEGIN
NEW."reminder" := make_date(CAST (extract(year from timestamp NEW."date_of_join") AS INTEGER) ,2,3);
RETURN NEW;
END 
$$ LANGUAGE 
plpgsql;

My trigger is :

CREATE TRIGGER "Trigger1"
BEFORE INSERT ON faculty
for each row
EXECUTE PROCEDURE update_reminder();

It is giving me error : syntax error at or near "NEW"

How to do this?

date_trunc can be used to round to the beginning of the year; then you can add 1 year to arrive at January first of the following year:

NEW.reminder := date_trunc('year', NEW.date_of_join) + INTERVAL '1 year';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM