How to get difference in two dates. Please HELP
Posted in 1998
Topics: Stored Procedures & SPL
Hi,
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.
I wrote a stored procedure to do this but it only works if the
difference in months is less than 100 months. The code is given below.
create procedure GetMonthDiff(st DATE, ed DATE)
returning int;
DEFINE s CHAR(20);
DEFINE numMonths INTERVAL MONTH TO MONTH;
let numMonths = EXTEND(st, YEAR TO MONTH) - EXTEND(ed, YEAR TO MONTH);
let s = numMonths;
return TRIM(s);
END PROCEDURE;
--
===========================================
Imran Hussain
MCP, MCSD
imranh@imranweb.com
http://www.imranweb.com
===========================================
Hi,
Thank you very much to every one for helping me out. After I posted the
message I tried a few more things and came up with the following
procedure. So far it is working fine. Please let me know if you think it
has any bug.
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;
--
===========================================
Imran Hussain
MCP, MCSD
imranh@imranweb.com
http://www.imranweb.com
===========================================