MySQL DATE_ADD Function

Monday, December 26, 2011

tags SQL

The MySQL DATE_ADD function adds a custom time interval to a date.

Employees Table

employeeIDemployeeNamehireDate
1000John Smith1995-12-03 13:23:30.657
1001Fred White2001-10-12 09:41:44.125
1002Jane Scott1998-05-01 11:36:16.334
1003Samuel Williams1991-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 NameRetirement Year
John Smith2025
Fred White2031
Jane Scott2028
Samuel Williams2021

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

SQL Cookbook Head First SQL: Your Brain on SQL A Learners Guide