Lesson

MySQL PERIOD_DIFF() Function

10 min reading
Free Course

MySQL PERIOD_DIFF() Function

The MySQL PERIOD_DIFF() function calculates the number of months between two periods formatted as YYMM or YYYYMM. It computes P1 - P2 and returns a signed integer.

Subscription platforms, payroll tables, and accounting ledgers often store accounting months as YYYYMM integers like 202605. PERIOD_DIFF() measures how many billing cycles elapsed between two periods without requiring string parsing or full DATE conversions.

If P1 comes after P2, the result is positive. If P1 comes before P2, the result is negative.

Syntax

PERIOD_DIFF(P1, P2)
  • P1: First period in YYMM or YYYYMM format.
  • P2: Second period in YYMM or YYYYMM format.

Practical Example

Suppose demo_customers records each account's signup_period in YYYYMM format. You can calculate how many months each customer has been active relative to period 202605.

SELECT
    customer_id,
    company_name,
    signup_period,
    PERIOD_DIFF(202605, signup_period) AS months_active
FROM demo_customers
WHERE signup_period >= 202501
ORDER BY months_active DESC;
customer_id company_name signup_period months_active
12 Northwind Labs 202502 15
19 Pixel Craft 202601 4
25 Cloud Harbor 202605 0

Notice that 202605 minus 202502 gives 15 months across the year boundary, rather than simple subtraction (202605 - 202502 = 103).

Common Mistakes & NULL Behavior

Never subtract YYYYMM integers directly with - in SQL. Base-10 subtraction gives 202601 - 202512 = 89 instead of 1 month. Always use PERIOD_DIFF().

Another common error is passing DATE values like '2026-05-01' or YYYYMMDD integers like 20260501 to PERIOD_DIFF(). The function expects YYMM or YYYYMM only; passing full dates yields incorrect month counts. Use TIMESTAMPDIFF(MONTH, d1, d2) for real DATE columns.

Also watch argument order: PERIOD_DIFF(P1, P2) returns P1 - P2, matching DATEDIFF(d1, d2) but opposite to TIMESTAMPDIFF(MONTH, d1, d2) which computes d2 - d1. PERIOD_DIFF() returns NULL if either P1 or P2 is NULL.

Frequently Asked Questions

What happens if P1 is earlier than P2 in PERIOD_DIFF()?

PERIOD_DIFF() returns a negative integer. For example, PERIOD_DIFF(202511, 202602) returns -3.

Can I mix YYMM and YYYYMM in the same PERIOD_DIFF() call?

Yes. MySQL converts two-digit years (00–69 to 2000s, 70–99 to 1900s) before computing the difference, though using YYYYMM for both is safer.

Which function should I use for DATE columns instead of PERIOD_DIFF()?

Use TIMESTAMPDIFF(MONTH, start_date, end_date) when your columns are DATE or DATETIME types.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum