원본출처 : http://www.brasileiro.net/postgres/cookbook/view-one-recipe.adp?recipe_id=21
Submitted on: 03-16-2001
Description:
Simple functions to add/subtract dates. Prototypes: DATE_ADD(DATE,INT,PERIOD) DATE_SUB(DATE,INT,PERIOD) Where PERIOD is one of 'DAYS','MONTHS',or 'YEARS'. examples: SELECT DATE_ADD('1999-12-31',1,'DAYS') will produce '2001-01-01'
Code:
-- DATE_ADD, DATE_SUB date manipulation functions
-- Copyright (C) 2001 Mark Steele (msteele@inet-interactif.com)
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
CREATE FUNCTION DATE_ADD(date,int4,text) RETURNS DATE AS '
DECLARE
var1 date;
var2 text;
BEGIN
var2 = $2 || '' '' || $3;
SELECT INTO var1
to_date($1 + var2::interval, ''YYYY-MM-DD'');
RETURN var1;
END;' LANGUAGE 'plpgsql';
CREATE FUNCTION DATE_SUB(date,int4,text) RETURNS DATE AS '
DECLARE
var1 date;
var2 text;
BEGIN
var2 = $2 || '' '' || $3;
SELECT INTO var1
to_date($1 - var2::interval, ''YYYY-MM-DD'');
RETURN var1;
END;' LANGUAGE 'plpgsql';
|