calculate date difference in monthwise in sqlserver I am using SQL SERVER. I want to calculate the exact date difference in month wise. I know there is one inbuilt function DATEDIFF for this but i want some custom fractional difference like: 

sDate(yyyy-MM-dd)  eDate          output              formula
---------------------------------------------------------------------------------------------------------------
2016-03-03        2016-03-05      0.0967741           3.00/31.00 (daycount/lastday of the month)
2016-03-05        2016-06-05      3.0376343          (27.00/31.00)+(30.00/30.00)+(31.00/31.00)+(5.00/30.00)
2016-02-26        2016-04-03      1.2379310          (4.00/29.00)+(31.00/31.00)+(3.00/30.00)


So how this will be calculated? I can apply this logic by using a function but I don't want to do so. How can I apply this logic in a single query?

Your valuable suggestion will be highly appreciated.
With the help of a mini calendar table, this can be handled pretty easily:

rextester setup: http://rextester.com/ZHTTZ86931

Testing data:

create table tmp (fromdate date,thrudate date,monthdiff decimal(19,7),formula varchar(64))
insert into tmp (fromdate, thrudate, monthdiff, formula) values
 ('2016-02-26','2016-04-03',1.2379310,'(4.00/29.00)+(31.00/31.00)+(3.00/30.00)')
,('2016-03-03','2016-03-05',0.0967741,'3.00/31.00 (daycount/lastday of the month)')
,('2016-03-05','2016-06-05',3.0376343
                         ,'(27.00/31.00)+(30.00/30.00)+(31.00/31.00)+(5.00/30.00)')
,('2016-01-01','2016-12-31',12,'');


Creating a mini calendar table to track each date and its percent of its month. 

  /* creating a DayPercentOfMonth table */
declare @FromDate date = '20100101';
declare @ThruDate date = '20301231';
create table dbo.DayPercentOfMonth (
    [Date] date not null primary key
  , DayPercentOfMonth decimal(9,8) not null
);
with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, cal as (
  select 
    Date =convert(date,dateadd(day, row_number() over (order by (select 1)) -1, @fromdate))
    , [DayPercentOfMonth] = convert(decimal(6,4),1)
                           /convert(decimal(6,4)
           ,datepart(day,dateadd(day,-1,dateadd(Month,datediff(Month,0
           ,row_number() over (order by (select 1)) -1)+1
           ,0))))
    from         n as deka
      cross join n as hecto
      cross join n as kilo     /* 2.73 years */
      cross join n as [tenK]    /* 27.3 years */
      --cross join n as [hundredk] /* 273  years */
    )
insert into dbo.DayPercentOfMonth ([Date],[DayPercentOfMonth])
select [Date],DayPercentOfMonth
from cal;


Alternate version if you really do not want to create the helper table, just uses the recursive cte:

/* set from &amp; thru dates for calendar cte */
declare @FromDate date = '20160101';
declare @ThruDate date = '20161231';

with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, cal as (
  select 
    Date =convert(date,dateadd(day, row_number() over (order by (select 1)) -1, @fromdate))
    , [DayPercentOfMonth] = convert(decimal(6,4),1)
                           /convert(decimal(6,4)
           ,datepart(day,dateadd(day,-1,dateadd(Month,datediff(Month,0
           ,row_number() over (order by (select 1)) -1)+1
           ,0))))
    from         n as deka
      cross join n as hecto
      cross join n as kilo     /* 2.73 years */
      cross join n as [tenK]    /* 27.3 years */
      --cross join n as [hundredk] /* 273  years */
    )


Then the query just joins the calendar where the calendar date is between the from and thru dates, and sums the DayPercentOfMonth column.

select 
    fromdate=convert(varchar(10),t.fromdate,120)
  , thrudate=convert(varchar(10),t.thrudate,120)
  , monthdiff=format(t.monthdiff ,'f8','en-US')
  , SumDayPercentOfMonth=format(convert(decimal(19,8),sum(DayPercentOfMonth)),'f8','en-US')
  --select fromdate, thrudate, monthdiff, SumDayPercentOfMonth=sum(DayPercentOfMonth)
  from tmp as t 
    inner join cal c on c.date &gt;= t.fromdate 
                    and c.date &lt;= t.thrudate
  group by t.fromdate, t.thrudate, t.monthdiff, t.formula
  order by fromdate

results in:

+------------+------------+-------------+----------------------+
|  fromdate  |  thrudate  |  monthdiff  | SumDayPercentOfMonth |
+------------+------------+-------------+----------------------+
| 2016-01-01 | 2016-12-31 | 12.0000000  | 12.00000000          |
| 2016-02-26 | 2016-04-03 | 1.2379310   | 1.23793103           |
| 2016-03-03 | 2016-03-05 | 0.0967741   | 0.09677419           |
| 2016-03-05 | 2016-06-05 | 3.0376343   | 3.03763441           |
+------------+------------+-------------+----------------------+


note: Sample data values for output were not as precisely correct as they might appear to be. 

(27.00/31.00)+(30.00/30.00)+(31.00/31.00)+(5.00/30.00) = 3.037634408602151 != 3.0376343
(3.00/31.00) = 0.0967741935483871 != 0.0967741 -- truncated