Datediff years to decimal places

I have two dates in which I would like to find the number of years between them, however I will need to show the value up to two decimal places. I tried the following, but always get a return value of 0, since all my dates do not span the whole year:

DATEDIFF(yy, @EndDateTime, i.mat_exp_dte)

Then I tried to find the number of days between them and then divide it by 365, but this still returns 0:

DATEDIFF(dd, @EndDateTime, i.mat_exp_dte)/365

Now confused how to calculate this. Do I need to convert a DataDiff to a different data type?

+6
source share
4 answers

Try this instead.

DATEDIFF(dd, @EndDateTime, i.mat_exp_dte)/365.0

Separating an int from an int returns an int. Divide with a decimal point, and as a result you get a decimal number.

+17
source

.0 , . .

+2

, , :

cast(DATEDIFF(dd, @EndDateTime, i.mat_exp_dte) as float)
/datediff(dd,@EndDateTime,dateadd(yy,1,@EndDateTime))
+2
source
SELECT
(YEAR(@EndDte) - YEAR(@StartDate) + 
(MONTH(@EndDate) - MONTH(@StartDate) /10.0 + 
(DAY(@EndDate) - DAY(@StartDate) /100.0)

If you need to determine if there is exactly one year, less than one or more than one. (= 1, <1,> 1)

0
source

All Articles