Subject | Re: Number of months between dates |
---|---|
Author | Ian A. Newby |
Post date | 2004-05-12T13:23:37Z |
Hi,
How about
CREATE PROCEDURE getMonths (
FDATE DATE,
TDATE DATE)
RETURNS (
MONTHS BIGINT)
AS
BEGIN
months =
(extract (year from tdate) * 12 + extract(month from tdate)) -
(extract (year from fdate) * 12 + extract(month from fdate))
+ case when (extract(day from tdate) > 15) then 0 else -1 end
+ case when (extract(day from fdate) < 15) then 1 else 0 end;
SUSPEND;
END
(Only works in Firebird 1.5 though!)
It can also be used in a select statement like
select (extract (year from tdate) * 12 + extract(month from
tdate)) -
(extract (year from fdate) * 12 + extract(month from fdate))
+ case when (extract(day from tdate) > 15) then 0 else -1 end
+ case when (extract(day from fdate) < 15) then 1 else 0 end
as months from mytable;
Regards
Ian Newby
How about
CREATE PROCEDURE getMonths (
FDATE DATE,
TDATE DATE)
RETURNS (
MONTHS BIGINT)
AS
BEGIN
months =
(extract (year from tdate) * 12 + extract(month from tdate)) -
(extract (year from fdate) * 12 + extract(month from fdate))
+ case when (extract(day from tdate) > 15) then 0 else -1 end
+ case when (extract(day from fdate) < 15) then 1 else 0 end;
SUSPEND;
END
(Only works in Firebird 1.5 though!)
It can also be used in a select statement like
select (extract (year from tdate) * 12 + extract(month from
tdate)) -
(extract (year from fdate) * 12 + extract(month from fdate))
+ case when (extract(day from tdate) > 15) then 0 else -1 end
+ case when (extract(day from fdate) < 15) then 1 else 0 end
as months from mytable;
Regards
Ian Newby