SQL > SQL Date Functions > Datepart Function

DATEPART is a SQL Server function that extracts a specific part of the date/time value.

DATEPART(part_of_day, expression) returns an integer representing the specified component — such as year, month, day, or day-of-year — from a date or datetime value in SQL Server.

Syntax

The syntax for the DATEPART function is as follows:

DATEPART (part_of_day, expression)

where part_of_day can have the following:

 datepart  abbreviation 
 year  yy, yyyy 
 quarter  qq, q 
 month  mm, m 
 dayofyear  dy, y 
 day  dd, d 
 week  wk, ww 
 hour  hh 
 minute  mi, n 
 second  ss, s 
 millisecond  ms 
 microsecond  mcs 
 nanosecond  ns 
 TZoffset  tz 
 ISO_WEEK  isowk, isoww 

Examples

Example 1

The SQL statement,

SELECT DATEPART (yyyy, '2000-01-20');

produces the following result:

2000

Example 2

The SQL statement,

SELECT DATEPART (dy, '2000-02-10');

produces the following result:

41

2000-02-10 is the 41st day in the year 2000.

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

What does the SQL DATEPART function do?

DATEPART extracts a specific component of a date or datetime value in SQL Server and returns it as an integer. Components include year, quarter, month, day, hour, minute, second, day-of-year, week, and more.

What is the syntax for DATEPART?

DATEPART (part_of_day, expression) — where part_of_day is the component to extract (e.g., yyyy for year, mm for month, dd for day) and expression is the date value.

What is the equivalent of DATEPART in other databases?

In Oracle and PostgreSQL, the EXTRACT function serves a similar purpose. MySQL supports both EXTRACT and dedicated functions like YEAR(), MONTH(), and DAY() for extracting date components.

What integer does DATEPART(dy, '2000-02-10') return?

It returns 41 — because February 10, 2000 is the 41st day of the year 2000.

Next: SQL GETDATE

This page was last updated on March 19, 2026.




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