This MySQL function returns the number of months between periods given.


Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).



This function returns the number of months between periods given. The periods given must be in string format and contain only the year and month, in either yyyymm or yymm format.

SELECT appointment AS 'Date of Appointment',
CURDATE( ) AS 'Current Date',
   EXTRACT(YEAR_MONTH FROM appointment),
) AS 'Accounting Periods Apart';

| Last Appointment | Current Date | Accounting Periods Ellapsed |
| 2008-11-15       | 2009-01-15   |                          -2 | 

This SQL statement has determined that it has been two months since the client's last appointment. If you want the results not to contain a negative, either switch the order of the periods or wrap the PERIOD_DIFF() within ABS(). The PERIOD_DIFF() function takes into account that the periods are in different years. But it doesn't work on standard date columns, so you have to put them into the proper string format as shown here with a function such as EXTRACT().