Awan Awan - 18 days ago 6
MySQL Question

How to calculate difference between two dates in months in MySQL

I have two columns in a MySQL table:


  • DateOfService (datetime)

  • BirthDate (date)



I want to run a MySQL query that will provide date difference between these two fields in months.

How can I do this in a MySQL select query?

Thanks.

Answer

This could work:

SELECT 12 * (YEAR(DateOfService) 
              - YEAR(BirthDate)) 
       + (MONTH(DateOfService) 
           - MONTH(BirthDate)) AS months 
FROM table