Calculate the number of years between dates in Sql

I need to calculate the number of full years between two dates in sql. The query that I use to calculate is

--Date1 > Date2
CASE
WHEN YEAR(Date1) = YEAR(Date2)
    THEN DATEDIFF(year, Date2, Date1)
WHEN YEAR(Date1) > YEAR(Date2) 
        AND MONTH(Date1) >= MONTH(Date2)
        AND DAY(Date1) >= DAY(Date2)
    THEN DATEDIFF(year, Date2, Date1)
ELSE
    DATEDIFF(year, Date2, Date1) - 1

However, I need to somehow configure it so that it considers the time between 1/5/2011 and 30/4/2012 as 1 full year.

+5
source share
2 answers

Add one day before Date2before comparing it:

dateadd(day, 1, Date2)
+4
source

If the goal is to consider a year during a year, not more than a year, but just add 1 day to a later date? dateadd () should do this.

mysql> SELECT ADDDATE('2011-05-01', INTERVAL 1 DAY);
        -> '2011-05-02'

I do not think this will cause other erroneous calculations ...

+1
source

All Articles