Re: How to get difference in two dates. Please HELP
Posted in 1998
Topics: Stored Procedures & SPL
Assuming you meant to write "dt3 = dt1 - dt2" . . . .
Why not try the following:
create procedure month_diff(d1 date, d2 date) returning decimal; return abs((d1 - d2) / 30);
end procedure
-mka
Michael Ackerbauer
SP System Test
Imran Hussain wrote:
>I need to subtract two dates from each other and get the difference in
>MONTHS not DAYS. So lets say I have the following:
>dt1 = '12/1/1998'
>dt2 = '7/15/1998'
>dt3 = dt1 - dt3 dt3 should be 5
>How do I do that.
Hi,
Thanks for your help. I don't think this method work correctly because not
every month is 30 days and as the difference increases the number of months
will become incorrect. After I posted the message, I tried a few things and
come up with the following solution. Please let me know if you find any
problem with it.
create procedure GetMonthDiff(st DATE, ed DATE)
returning int;DEFINE answer int;
LET answer = MONTH(st) - MONTH(ed) +
(YEAR(st) - YEAR(ed)) * 12;
return answer;
END PROCEDURE;
ackerbau@us.ibm.com wrote:
> Assuming you meant to write "dt3 = dt1 - dt2" . . . .
>
> Why not try the following:
>
> create procedure month_diff(d1 date, d2 date) returning decimal;> return abs((d1 - d2) / 30);
> end procedure
>
> -mka
>
--
===========================================
Imran Hussain
MCP, MCSD
imranh@imranweb.com
http://www.imranweb.com
===========================================