DATEPART (Transact‑SQL)
Returns an integer representing the specified part of a date.
Syntax
DATEPART ( datepart , date )
Parameters
Parameter | Description |
---|---|
datepart | Part of the date to return. See datepart values. |
date | An expression that can be resolved to a datetime , datetime2 , smalldatetime , date , time , or datetimeoffset value. |
Return Types
Returns int
.
Remarks
- The
datepart
argument can be any of the date parts listed in the Datepart arguments table. - If
date
isNULL
, the function returnsNULL
.
Examples
Example 1 – Return the month from a date
SELECT DATEPART(month, '2019-07-25') AS MonthNumber;
Example 2 – Using DATEPART with GETDATE
SELECT DATEPART(weekday, GETDATE()) AS TodayWeekday;