Awan Awan - 1 year ago 95
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?


Answer Source

This could work:

SELECT 12 * (YEAR(DateOfService) 
              - YEAR(BirthDate)) 
       + (MONTH(DateOfService) 
           - MONTH(BirthDate)) AS months 
FROM table
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download