Datepart Function
Next: SQL GETDATE
DATEPART is a SQL Server function that extracts a specific part of the date/time value. Its syntax 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 |
Example 1:
SELECT DATEPART (yyyy,'2000-01-20');
Result:
2001
Example 2:
SELECT DATEPART(dy, '2000-02-10');
Result:
41
2000-02-01 is the 41st day in the year 2000.