/ Published in: SQL
Add a given interval to a date. The intervals can be year, month or day
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
CREATE OR REPLACE FUNCTION dateadd(diffType CHARACTER VARYING(15), incrementValue INT, inputDate TIMESTAMP) RETURNS TIMESTAMP AS $$ DECLARE YEAR_CONST CHAR(15) := 'year'; MONTH_CONST CHAR(15) := 'month'; DAY_CONST CHAR(15) := 'day'; dateTemp DATE; intervalValue VARCHAR (100); BEGIN IF LOWER($1) = LOWER(YEAR_CONST) THEN dateTemp := inputDate + INTERVAL '$2 year'; ELSEIF LOWER($1) = LOWER(MONTH_CONST) THEN dateTemp := inputDate + INTERVAL '$2 months'; ELSEIF LOWER($1) = LOWER(DAY_CONST) THEN dateTemp := inputDate + INTERVAL '$2 day'; END IF; RETURN dateTemp; END; $$ LANGUAGE plpgsql;