Return to Snippet

Revision: 4051
at October 19, 2007 04:26 by arunpjohny


Initial Code
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;

Initial URL

                                

Initial Description
Add a given interval to a date. The intervals can be year, month or day

Initial Title
Add a interval to a given date in postgresql

Initial Tags

                                

Initial Language
SQL