The MySQL EXTRACT Function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.
Employees Table
employeeID | employeeName | accountCreated |
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' accounts were created.
Syntax
EXTRACT(format FROM date)
|
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], EXTRACT(YEAR FROM accountCreated) as [Year Acc Created]
FROM employees
|
Results
Employee Name | Year Acc Created |
John Smith | 1995 |
Fred White | 2001 |
Jane Scott | 1998 |
Samuel Williams | 1991 |
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources