YEARFRAC equivalent for SSRS?

I am trying to replicate the YEARFRAC feature in EXCEL into SSRS.

Here's the expression in my report, but it's not returning the same results as in Excel. I'm using DateDiff...

Open in new window

I believe the results would be in decimals.... the YEARFRAC in Excel basically gets the fraction of a year between two dates
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
For the benefit of those SQL Server experts that are not familiar with the Excel function YEARFRAC, can you provide us with 'before and after' sample data of what you are trying to pull off here?

Thanks in advance.
teknovationAuthor Commented:
Hi Jim, please see below.


Open in new window

Sample debug code:

10.500 is the result using the expression above when it should be 19.636

The two dates are 12/20/2012 and 01/15/2014
The total value is: 21
teknovationAuthor Commented:
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Jim HornMicrosoft SQL Server Data DudeCommented:
This will probably get you the same level of accuracy as the Excel YEARFRAC
=21/(DateDiff("d",Fields!FirstDate.Value,Fields!SecondDate.Value) / 365) 

Open in new window

However many developers would propose that logic like this should be in SQL, in the stored procedure that serves up the SSRS dataset...
= 21 / DATEDIFF(FirstValue, SecondValue) / 365.0) as column_name

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
teknovationAuthor Commented:
thanks ill try it. but its for crm dynamic which is fetch xml, we dont have sql server
teknovationAuthor Commented:
Hi Jim, thanks so much for the formula! It was pretty darn close, the values were off by .03 but good enough, unless you have something else that can make it more accurate!
Jim HornMicrosoft SQL Server Data DudeCommented:
My guess is that it's as accurate as the Excel YEARFRAC function, and the only way to test that is to do a side-by-side comparison.  If you've already done that to get to the .03 it would be worth posting it here.
teknovationAuthor Commented:
=21/(DateDiff("d",Fields!FirstDate.Value,Fields!SecondDate.Value) / 365) 

Open in new window

Was able to get me this close to the YEARFRAC results which is the value to the right side.
19.556 vs 19.588

49.183 vs 49.169
Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the grade.  Good luck with your project.  -JIm

btw Check this out, it was just published a half an hour ago
Top 10 Ways to Write Rock Star Articles
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.