0
0
MysqlHow-ToBeginner · 3 min read

How to Use DATE_ADD in MySQL: Syntax and Examples

In MySQL, use the DATE_ADD(date, INTERVAL value unit) function to add a specific time interval to a date. For example, DATE_ADD('2024-06-01', INTERVAL 5 DAY) adds 5 days to the given date.
📐

Syntax

The DATE_ADD function adds a time interval to a date or datetime value.

  • date: The starting date or datetime.
  • INTERVAL value unit: The amount and type of time to add (e.g., days, months, years).
sql
DATE_ADD(date, INTERVAL value unit)
💻

Example

This example adds 10 days to the date '2024-06-01'. It shows how DATE_ADD returns the new date after adding the interval.

sql
SELECT DATE_ADD('2024-06-01', INTERVAL 10 DAY) AS new_date;
Output
new_date 2024-06-11
⚠️

Common Pitfalls

Common mistakes include:

  • Forgetting to use the INTERVAL keyword before the value and unit.
  • Using incorrect units like 'days' instead of 'DAY'.
  • Passing invalid date formats.

Always use uppercase units like DAY, MONTH, YEAR.

sql
/* Wrong usage: missing INTERVAL keyword */
SELECT DATE_ADD('2024-06-01', 10 DAY);

/* Correct usage */
SELECT DATE_ADD('2024-06-01', INTERVAL 10 DAY);
📊

Quick Reference

UnitDescriptionExample
SECONDAdd secondsINTERVAL 30 SECOND
MINUTEAdd minutesINTERVAL 15 MINUTE
HOURAdd hoursINTERVAL 5 HOUR
DAYAdd daysINTERVAL 10 DAY
WEEKAdd weeksINTERVAL 2 WEEK
MONTHAdd monthsINTERVAL 3 MONTH
YEARAdd yearsINTERVAL 1 YEAR

Key Takeaways

Use DATE_ADD(date, INTERVAL value unit) to add time intervals to dates in MySQL.
Always include the INTERVAL keyword before the value and unit.
Use uppercase time units like DAY, MONTH, YEAR for correct syntax.
DATE_ADD works with date and datetime values to return a new date.
Common errors come from missing INTERVAL or using invalid units.