DATEPART
DATEPART is a function in SQL that is used to return a specific part of a date, such as the year, month, day, hour, minute or second.
DATEPART( datepart , date )
- datepart: This parameter specifies the part of the date that the function will return. It can be year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond, tzoffset, or iso_week.
- date: This parameter is the date value from which the function extracts the specified date part. It can be of date, smalldatetime, datetime, datetime2, datetimeoffset, or time data types.
Example
SELECT DATEPART(year, '2021-10-01') AS Year;
Output
Year---------2021
Explanation
The DATEPART function extracts the specified part of the date (in this case ‘year’) from a given date. Here, the function returns ‘2021’ after extracting the year from ‘2021-10-01’.