Link to home
Start Free TrialLog in
Avatar of venussc
venussc

asked on

SQL Server - How to Calculate the number of fractional months between two dates (UDF)

I want to create a UDF that  Calculates the number of fractional months between two dates.
E.g. from 01/01/2008 to 14/02/2008 = 1.542 Months.

I have tried using  Datediff function but that has some issues.

First issue :
It only returns whole months.

second Issue :
The DateDiff function will return the number of month boundaries between 2 dates.
so, 01/01/2008 to 31/01/2008 will return 0. This i have fixed by adding 1 to end date.
but since I cannot get fractions 01/01/2008 to 15/01/2008 returns 0 instead of around 0.5.

Third Issue :
The function should handle the dates even if they span over multiple years e.g 02/03/2006 to 02/03/2008 = 24 months

Kindly give some code to do this. Thanks.  More info:
The function will be used to calculate the number of leave someone has earned at the of 1.75 days per month. so if someone starts employment on the (dd/mm/yyyy) 01/01/2008, on the 01/02/2008 he would have 1 month x 1.75 days of leave which is  = 1.75
similarly if sumone starts on the 15/01/2008, on the 01/02/2008 he would have earned 0.5 of a month x 1.75 = 0.875 days. So thats why I require fractional months.
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

did you try using datediff with days and the divide the days by 30 ?
Avatar of venussc
venussc

ASKER

Tried that. The problem with that is to get exact number of days. some months has 28/29/30/ or 31 days. The logic being from 15/01/2008 to 15/02/2008 = 1 month and have 31 days but 15/02/2008 to 15/03/2008 = 1 moth but has 28 days
well, that is pretty much your only option, since the datediff function will only return whole months...
Avatar of venussc

ASKER

Awaiting more feedback please
Avatar of Patrick Matthews
Hello venussc,

DATEDIFF when used with a monthly interval does *not* return the number of whole months between two
datetimes.  Rather, it returns the number of *month boundaries* between the two datetimes.

For example:

SELECT DATEDIFF(month, '2009-01-31', '2009-02-01')            --- >      1

SELECT DATEDIFF(month, '2009-01-31', '2009-03-01')            --- >      2

Those results are certainly not "whole month" :)

Regards,

Patrick
ASKER CERTIFIED SOLUTION
Avatar of tigin44
tigin44
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of venussc

ASKER

Thanks Guru tigin44. I think with this method I can adapt to whatever exact condition I will need.
tigin44,

Brilliant answer!  I did a little testing, and found the following quirks:

1) In the unlikely event that dates from 1900 or 2100 are fed in, the results may be wrong by a tiny amount:
years divisible by 100 but *not* divisible by 400 are standard years, not leap years.

2) Depending on how the Asker defines "whole months", there may be an issue with a start date of 31 Jan
and end date of 1 Mar (for any year).  For leap years the answer your SQL returns is 0.9655, and for
standard years it's 0.9286.  That may be what the Asker wants, but then again it could be that the Asker
would want to see a number slightly greater than 1 (as all of February is encompassed in the time period).

Regards,

Patrick
Avatar of venussc

ASKER

Nice Spotting matthewspatrick, I had tested with quite a few dates but not what you used. I can indeed replicate your results of 0.9655.
I indeed expect to get 1.0322 or something like that as the result for your above dates. matthewspatrick do you see any way to adjust the function to do that.

Thanks again for your observation.