Concept Flow - DATE_ADD and DATE_SUB
Start with a date
Choose operation: Add or Subtract
DATE_ADD
Add interval
Return new date
End
Start with a date, choose to add or subtract a time interval, then get the new date as result.
SELECT DATE_ADD('2024-06-01', INTERVAL 5 DAY) AS added_date; SELECT DATE_SUB('2024-06-01', INTERVAL 3 DAY) AS subtracted_date;
| Step | Function | Input Date | Interval | Operation | Result |
|---|---|---|---|---|---|
| 1 | DATE_ADD | 2024-06-01 | 5 DAY | Add 5 days | 2024-06-06 |
| 2 | DATE_SUB | 2024-06-01 | 3 DAY | Subtract 3 days | 2024-05-29 |
| 3 | END | - | - | - | Execution complete |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| Input Date | 2024-06-01 | 2024-06-01 | 2024-06-01 | 2024-06-01 |
| Added Date | N/A | 2024-06-06 | 2024-06-06 | 2024-06-06 |
| Subtracted Date | N/A | N/A | 2024-05-29 | 2024-05-29 |
DATE_ADD(date, INTERVAL n unit) adds time to a date. DATE_SUB(date, INTERVAL n unit) subtracts time from a date. Units can be DAY, MONTH, YEAR, etc. Returns a new date shifted by the interval. Useful for date calculations in SQL.
DATE_ADD(date, INTERVAL expr unit) do?DATE_ADD is designed to add a time interval to a given date value.DATE_SUB, not DATE_ADD. The options for returning the current date and time and formatting a date into a string describe unrelated functions.DATE_SUB with INTERVAL keyword and unit DAY.DATE_SUB('date', INTERVAL 10 DAY). DATE_ADD('2024-06-15', INTERVAL -10 DAY) uses DATE_ADD with negative interval, which is not recommended. DATE_SUB('2024-06-15', 10 DAY) misses INTERVAL keyword. DATE_ADD('2024-06-15', 10 DAYS) misses the INTERVAL keyword and uses DATE_ADD which adds instead of subtracts.SELECT DATE_ADD('2024-06-10', INTERVAL 3 MONTH) AS new_date;SELECT DATE_SUB('2024-06-20', 5 DAY);DATE_SUB(date, INTERVAL expr unit).DATE_SUB('2024-06-20', 5 DAY) missing INTERVAL, causing syntax error.LAST_DAY(CURDATE()) gives the last day of this month.DATE_SUB(..., INTERVAL 2 WEEK). SELECT DATE_SUB(LAST_DAY(CURDATE()), INTERVAL 2 WEEK) AS result; uses this correctly. SELECT DATE_ADD(LAST_DAY(CURDATE()), INTERVAL -2 WEEK) AS result; uses DATE_ADD instead of DATE_SUB. SELECT DATE_SUB(CURDATE(), INTERVAL 2 WEEK) AS result; subtracts from current date, not last day. SELECT DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 2 WEEK) AS result; adds 2 weeks, which is incorrect.Pass the test and this pattern moves unknown