Your Knowledge Base has moved to the new Help Center.  Check out the release notes for details. And don't forget to update your bookmarks and in-house documentation before May 28.

DATEPART

 

DATEPART returns part of a date as an integer.

Syntax

DATEPART( <date-part>, <date> )

Details

  • Date Part — Determines the type of units to extract from the date (see below).
  • Date — The date to retrieve a part from.
Date Parts
  • Year — yy (or yyyy)
  • Month — m (or mm)
  • Day — d (or dd)
  • Hour — hh
  • Minute — mi (or n)
  • Quarter — q (or qq)
  • Day of Year — dy (or y)
  • Week — wk (or ww)
  • Weekday — dw (or weekday)
Week/Weekday

The Week and Weekday uses the first day of the week set in SQL Server. The U.S. English default is Sunday, but it can be changed.

To find the current setting you can use:

@@DATEFIRST

Because the value can be changed, getting the week or weekday is not always reliable. One way to do it reliably is to use DATENAME

Examples

The year for the current date:

DATEPART(yy,GETDATE())

The Month for the current date:

DATEPART(mm, GETDATE())

Resources