Skip site navigation (1) Skip section navigation (2)

Adding to a date

From: "Mike Ellsworth" <younicycle(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Adding to a date
Date: 2008-06-30 21:44:09
Message-ID: 219951fd0806301444v621aff61iacdb5e46a0481e5f@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
I was beginning to create some financial functions for calculating
Future Values, PV's, etc  -for basic retirement planning.
example:

CREATE OR REPLACE FUNCTION lotsa.FVPMT(payment double precision,
interestRate double precision, periods double precision)
RETURNS  double precision AS
$BODY$
 BEGIN
return payment*(pow(1+interestRate, periods)-1)/interestRate;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE

periods seems to be a problem.  Usually, I'd be getting birth_date
from a file, but I'm not finding a way to
add 65 years - or 67 or whatever, to get the period between now (or an
assumed date) and retirement.

I'm hoping there is a way to:
1) Date of birth + xx years - with a result of date (Result 1)
2) Subtract now or an assumed date from Result 1 to get Result 2
3) Divide by ~ 365.25 for years, which would be 'periods' in the function.

I've gotten around it, but it sure seems pretty messy:
Greatest(FVPMT(test_fv.pmt, test_fv.i_rate,
(23741.25-(test_fv.start_date-test_fv.dob))/365.25),0)

where the 23741.25 is 65* 365.25.

Any help would be appreciated.  I've read all of what would seem to be
appropriate.
Doesn't mean I understood it, but I read it.

I'm not concerned about being off by a day or 2.
Small potatoes compared to the variations rate of return will undergo.

Thanks

Responses

pgsql-novice by date

Next:From: Harold A. Giménez Ch.Date: 2008-06-30 21:52:56
Subject: Fwd: Adding to a date
Previous:From: Bruce MomjianDate: 2008-06-30 19:52:08
Subject: Re: [NOVICE] encoding problems

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group