Sample Header Ad - 728x90

MySQL - How to make a DATEDIFF in years?

11 votes
2 answers
35694 views
In my table subventions (grants), I created a trigger to be able to calculate the annual amount of money of a grant, using my columns subventions.début (beginning), subventions.fin (end) and subventions.montant (amount). As DATEDIFF only gives me an answer in days, I naively thought that I only had to divide it by 365 days to obtain my answer. It works if my beginning date is the first of january and the end date the 31st of December, but does not if it stars somewhere else during the year. Here is my trigger: SET NEW.montant_annuel = NEW.montant / (datediff(NEW.fin,NEW.début)/365) Here is my table and the result in the 'montant_annuel' column: Tableau : Subventions If I use 365.25 (which is the "real" number of days in a year), it does not work either.
Asked by MDF (113 rep)
Aug 28, 2015, 04:43 PM
Last activity: Jun 1, 2025, 09:01 PM