Subject Re: Number of months between dates
Author Svein Erling
Raigo, I'd rather say you made it pass your test than actually make it
work. I cannot make sense of your computations. Let's say that I
started working 12 April 2003 and quit 12 May 2004, that should be
exactly 13 months. Here's your computation:

Years = 1
Months = 1
worked_months = 13

Then we start on the part of your code that I do not understand:

>If (LDay<15) then worked_months=worked_months-1;

worked_months now = 12

If (ADay<=15) then worked_months=worked_months+1;

worked_months back to 13

If ((ADay<=15) and (LDay<15) and (AMonth<>LMonth) and (AYear<>LYear))
then worked_months=worked_months+1;

Whoops, this is also true in my case so I get up to worked_months =
14! Maybe I shouldn't tell my boss that I actually only worked 13
months?

Your problem is actually very simple. Just start off as if everything
was inclusive:

worked_months=(years*12)+months+1;

and then get back to your original code:

If (ADay>15) then worked_months=worked_months-1;
If (LDay<=15) then worked_months=worked_months-1;

That's it, problem solved (if I understood your requirement correctly)
.

I hope this helps, if not I'm sure some members of The Firebird
Foundation can help you at Fulda (and probably a lot of non-members as
well, but the point of this paragraph was simply to tell people that
it is possible to apply for membership at http://www.firebirdsql
org/ff/foundation).

Set

--- In firebird-support@yahoogroups.com, "Raigo" wrote:
> Thanks for everybody. I got it to work :)
>
> procedure is now following:
>
> CREATE PROCEDURE SP_PUHKUS (
> BEGIN_DATE TIMESTAMP,
> END_DATE TIMESTAMP
> ) RETURNS (
> WORKED_MONTHS INTEGER
> ) AS
>
> declare variable ADay Integer;
> declare variable AMonth Integer;
> declare variable AYear Integer;
>
> declare variable LDay Integer;
> declare variable LMonth Integer;
> declare variable LYear Integer;
> declare variable years Integer;
> declare variable months Integer;
>
> BEGIN
>
> ADay = EXTRACT(DAY from begin_date);
> AMonth = EXTRACT(MONTH from begin_date);
> AYear = EXTRACT(YEAR from begin_date);
>
> LDay = EXTRACT(DAY from end_date);
> LMonth = EXTRACT(MONTH from end_date);
> LYear = EXTRACT(YEAR from end_date);
>
> years=LYear-AYear;
> months=LMonth-AMonth;
> worked_months=(years*12)+months;
>
> If (LDay<15) then worked_months=worked_months-1;
> If (ADay<=15) then worked_months=worked_months+1;
> If ((ADay<=15) and (LDay<15) and (AMonth<>LMonth) and
(AYear<>LYear)) then worked_months=worked_months+1;
>
>
>
> SUSPEND;
> END