Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Months difference with days precision

Status
Not open for further replies.

HugoBerg

Programmer
Jan 30, 2002
6
CA
Good morning group,

I need to get months differences but with days precision. Let's say we have an employee with a Service Date of May 21 1999, if my reference date is May 8 2004, I need to return a value of 59 months. Only when I'll reach the date of May 21 2004 should I get a value of 60.

I'm on Sybase and I use the built-in datediff function, but this function just doesn't care about the day. datediff (mm, 'May 21 1999', 'May 08 2004') returns 60 and that's not what I want.

I'm about to try the CASE function to get my result. Something like: If Day2 < Day1 then Months = Months - 1, but there must be a straightforward way of getting that 59...?

TIA.

Hugo.
 
That gets the work done:

... AND E.SERVICE_INTERVALS <=
(CASE when
(datepart(dd,A.ACCRUAL_PROC_DT)<datepart(dd,D.SERVICE_DT))
then(datediff(mm,D.SERVICE_DT,A.ACCRUAL_PROC_DT)-1)
else(datediff (mm, D.SERVICE_DT, A.ACCRUAL_PROC_DT))
end)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top