Subject | Years, Months & Days Between Dates |
---|---|
Author | LtColRDSChauhan |
Post date | 2014-04-11T11:59:43Z |
1. How can I please find Years, Months & Days between two dates.
2. For the above purpose, is my stored procedure listed as under, good ?
SET TERM ^ ;
ALTER PROCEDURE YYMMDD_BETN_DTS (
DT_FROM timestamp,
DT_TO timestamp )
RETURNS (
YY bigint,
MM bigint,
DD bigint )
AS
declare variable DT timestamp;
declare variable iSign smallint default 1;
BEGIN
/* write your code here */
if(DT_FROM > DT_TO) then
begin
DT = DT_FROM;
DT_FROM = DT_TO;
DT_TO = DT;
iSign = -1;
end
YY = datediff(year from :DT_FROM to :DT_TO);
ALTER PROCEDURE YYMMDD_BETN_DTS (
DT_FROM timestamp,
DT_TO timestamp )
RETURNS (
YY bigint,
MM bigint,
DD bigint )
AS
declare variable DT timestamp;
declare variable iSign smallint default 1;
BEGIN
/* write your code here */
if(DT_FROM > DT_TO) then
begin
DT = DT_FROM;
DT_FROM = DT_TO;
DT_TO = DT;
iSign = -1;
end
YY = datediff(year from :DT_FROM to :DT_TO);
DT_FROM = dateadd ( YY year to DT_FROM);
if(DT_FROM > DT_TO) then
begin
YY = YY - 1;
DT_FROM = dateadd ( -1 year to DT_FROM);
end
MM = datediff(month from :DT_FROM to :DT_TO);
DT_FROM = dateadd ( MM month to DT_FROM);
if(DT_FROM > DT_TO) then
begin
MM = MM - 1;
DT_FROM = dateadd ( -1 month to DT_FROM);
end
DD = datediff(day from :DT_FROM to :DT_TO);
YY = iSign * YY;
MM = iSign * MM;
DD = iSign * DD;
suspend;
/* test cases :-
('31.12.2013', '1.1.2014') : DD = 1, MM = 0, YY = 0
('13.3.1964', 'Now') : DD = 29, MM = 0, YY = 50
*/
END^
SET TERM ; ^
if(DT_FROM > DT_TO) then
begin
YY = YY - 1;
DT_FROM = dateadd ( -1 year to DT_FROM);
end
MM = datediff(month from :DT_FROM to :DT_TO);
DT_FROM = dateadd ( MM month to DT_FROM);
if(DT_FROM > DT_TO) then
begin
MM = MM - 1;
DT_FROM = dateadd ( -1 month to DT_FROM);
end
DD = datediff(day from :DT_FROM to :DT_TO);
YY = iSign * YY;
MM = iSign * MM;
DD = iSign * DD;
suspend;
/* test cases :-
('31.12.2013', '1.1.2014') : DD = 1, MM = 0, YY = 0
('13.3.1964', 'Now') : DD = 29, MM = 0, YY = 50
*/
END^
SET TERM ; ^
GRANT EXECUTE
ON PROCEDURE YYMMDD_BETN_DTS TO SYSDBA WITH GRANT OPTION;
--
thanks & Regards,
Lt Col (Retd) Rajiv D.S. Chauhan
_____________________________