SQL > SQL Date Functions > EXTRACT Function

The EXTRACT function is used to retrieve a specific component of a date or a timestamp. This function is available in several databases such as MySQL, Oracle, DB2, PostgreSQL, and Google BigQuery. Note that this function is not available in SQL Server and Hive.

The SQL EXTRACT function pulls a single numeric component (year, month, day, hour, etc.) out of a date or datetime value — making it easy to filter, group, or display specific parts of a date without string manipulation.

Syntax

The syntax for the EXTRACT function is as follows:

EXTRACT (component_part FROM expression)

where the data type of <expression> is some type of date, time, or datetime. <number> is an integer (can be positive or negative). Common <component_datepart> can be one of the following:

  • YEAR
  • MONTH
  • DAY
  • HOUR
  • MINUTE
  • SECOND

Different databases may offer other component_part types. For example, in Google BigQuery, you can specify DAYOFWEEK to find out the day of the week. This is not available in Oracle or MySQL.

Please note that component_part needs to align with the data type of expression. For example, if you try to extract the hour from a DATE expression, you will get an error.

Examples

Below we show several examples of the output from applying the EXTRACT function. Please note that we assume that expression is already in the correct data type (either DATE, DATETIME, or TIMESTAMP). In addition, in Oracle you will need to add "FROM DUAL" at the end of the statement.

Example 1

The SQL statement,

SELECT EXTRACT(YEAR FROM '2022-01-01');

produces the following result:

2022

Example 2

The SQL statement,

SELECT EXTRACT(MONTH FROM '2022-01-01 15:12:35');

produces the following result:

1

Example 3

The SQL statement,

SELECT EXTRACT(HOUR, '2022-01-01 15:12:35');

produces the following result:

15

List of SQL Date Functions

Function NameDescription
 DATEADD  Adds an interval to a date value in SQL Server. 
 DATEDIFF  Calculates the difference between two dates in MySQL and SQL Server. 
 DATEPART  Extracts a specific part of a date/time value in SQL Server. 
 GETDATE  Retrieves database time in SQL Server. 
 SYSDATE  Retrieves database time in Oracle and MySQL. 
 EXTRACT  Retrieves a certain component of a date or timestamp value. 

Frequently Asked Questions

Q: What does the SQL EXTRACT function do?
A: EXTRACT retrieves a specific component (such as YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND) from a date, time, or datetime expression.

Q: Is EXTRACT available in SQL Server?
A: No. SQL Server does not support EXTRACT. Use the DATEPART() function instead, e.g., DATEPART(year, date_column).

Q: Can you extract HOUR from a DATE column?
A: No. You can only extract components that are part of the data type. Trying to extract HOUR from a DATE (which has no time component) will result in an error.

Q: How does EXTRACT differ from date formatting functions?
A: EXTRACT returns a numeric integer value (e.g., 2022 for a year). Date formatting functions like DATE_FORMAT return a string representation of the date.

Next: Data Definition Language (DDL)

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact