The MySQL DATE_ADD
function adds a custom time interval to a date.
Employees Table
employeeID | employeeName | hireDate |
---|---|---|
1000 | John Smith | 1995-12-03 13:23:30.657 |
1001 | Fred White | 2001-10-12 09:41:44.125 |
1002 | Jane Scott | 1998-05-01 11:36:16.334 |
1003 | Samuel Williams | 1991-01-03 15:19:51.293 |
In this example, we want to find out what year the employees’ are eligible for retirement.
Syntax
DATE_ADD(date,INTERVAL interval format)
Format
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH
Example
SELECT employeeName as [Employee Name], DATE_ADD(hireDate INTERVAL 30 YEAR) as [Retirement Year]
FROM employees
Results
Employee Name | Retirement Year |
---|---|
John Smith | 2025 |
Fred White | 2031 |
Jane Scott | 2028 |
Samuel Williams | 2021 |