Did you find this useful?
Socialize it today.


MySQL EXTRACT Function

Sunday, December 25, 2011

tags SQL

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

employeeIDemployeeNameaccountCreated
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' 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 NameYear Acc Created
John Smith1995
Fred White2001
Jane Scott1998
Samuel Williams1991

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

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