Subject RE: [firebird-support] How to count leap days from date range.
Author Svein Erling Tysvær
To find the number of leap years, you can try

DATE_TO - DATE_FROM -
(extract(year from DATE_TO) - extract(year from DATE_FROM)) * 365 -
(case when extract(day from DATE_TO) = 29 and extract(month from DATE_TO) = 2 then cast('28.02.1999' as Date)
else cast(extract(day from DATE_TO) || '.' || extract(month from DATE_TO) || '.1999' as Date) end -
case when extract(day from DATE_FROM) = 29 and extract(month from DATE_FROM) = 2 then cast('01.03.1999' as Date)
else cast(extract(day from DATE_FROM) || '.' || extract(month from DATE_FROM) || '.1999' as Date)) end

The first line will (of course) get you the number of days between the two dates (unless yahoogroups removes my line breaks), the last four lines will get you the number of days it would have been if there were no leap years. The difference gets you how many 'leap days' between two dates (including DATE_FROM and DATE_TO if either of them happen to be 29 February, if I did this correctly). I'll leave it up to you to figure out whether I should get different interest if I deposit my 3 euro 28 February 2012 and withdraw them 28 February 2013, compared to if I deposit them 1 March 2011 and withdraw them 1 March 2012. Theoretically, in the first case, I assume that the interest should be divided by 366 for the first 9 months and by 365 for the last three, in the latter case, I'd expect divided by 365 for the first 9 months and 366 for the last three. Anyway, when Martijn cannot trust me, I will not risk putting Martijns money into a Slovakian bank and rather invest them in some Bamsemums (Norwegian sweets that have earned Firebird Foundation a small fortune).

HTH,
Set

-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Roland Turcan
Sent: 11. februar 2009 21:13
To: firebird-support@yahoogroups.com
Subject: [firebird-support] How to count leap days from date range.

Hello firebird-support@yahoogroups.com!

I have two dates and I need to count, that how many times occured
29.2. between two days.

Why:

I need to calculate year interest, but:

INTEREST_AMOUNT = ISTINA * (DATE_FROM - DATE_TO + 1) * INTEREST / 365 / 100;

...increases the count of days with leap days which gives wrong
computation.

How could I solve this computation using FireBird 2.1 with dialect 3
in stored procedure, but WITHOUT creating any new UDF funtion.

Thanks for all hints in advance.

--
Best regards, TRoland
http://www.rotursoft.sk
http://exekutor.rotursoft.sk