Lesson

MySQL SUBDATE() Function

10 min reading
Free Course

MySQL SUBDATE() Function

The MySQL SUBDATE() function subtracts a time interval or a number of days from a DATE or DATETIME expression. It supports two forms: full INTERVAL syntax (matching DATE_SUB()) and a concise integer days shorthand.

Developers use SUBDATE() to compute lookback windows, invoice grace periods, trial expiration dates, and rolling 7-day or 30-day analytics queries.

Unlike DATE_SUB(), SUBDATE() lets you write SUBDATE(order_date, 7) directly without typing INTERVAL 7 DAY.

Syntax

SUBDATE(date, INTERVAL expr unit)
SUBDATE(expr, days)
  • date / expr: A valid DATE or DATETIME expression.
  • expr unit: Quantity and unit (such as INTERVAL 14 DAY, INTERVAL 2 MONTH, or INTERVAL '1:30' HOUR_MINUTE).
  • days: An integer number of days to subtract.

Practical Example

In demo_orders, you can find a customer's 30-day refund cutoff date alongside a 2-hour cancellation window using both SUBDATE() syntaxes.

SELECT
    order_id,
    order_date,
    SUBDATE(order_date, 30) AS thirty_days_prior,
    SUBDATE(created_at, INTERVAL 2 HOUR) AS two_hours_prior
FROM demo_orders
WHERE order_id IN (801, 802);
order_id order_date thirty_days_prior two_hours_prior
801 2026-05-10 2026-04-10 2026-05-10 11:20:00
802 2026-03-01 2026-01-30 2026-02-28 23:15:00

Notice how SUBDATE('2026-03-01', 30) crosses February (28 days in 2026) accurately to land on '2026-01-30'.

Common Mistakes & NULL Behavior

Watch what happens when subtracting MONTH from end-of-month dates. SUBDATE('2026-03-31', INTERVAL 1 MONTH) returns '2026-02-28' because February has no 31st day. Chaining monthly subtractions can drift toward day 28 if you are not careful.

Another mistake is passing INTERVAL 7 DAY versus an integer 7 to DATE_SUB(). Only SUBDATE() and ADDDATE() support the integer shorthand; DATE_SUB(col, 7) causes a syntax error.

SUBDATE() returns NULL if either argument is NULL, or if the starting date is invalid (such as '2026-02-30'). Adding or subtracting time units like HOUR on a DATE automatically promotes the return value to DATETIME.

Frequently Asked Questions

What is the difference between SUBDATE() and DATE_SUB()?

With INTERVAL syntax, they are identical. SUBDATE() additionally supports an integer second argument like SUBDATE(my_date, 7) to subtract days.

Can I pass a negative number of days to SUBDATE()?

Yes. SUBDATE('2026-05-10', -5) subtracts -5 days, effectively adding 5 days to return '2026-05-15'.

Does SUBDATE() modify the stored table value?

No. Like other MySQL scalar functions, SUBDATE() computes a new value in query results unless used inside an UPDATE statement.

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