SQL DATEPART() function is used to get a part of any specific date , it could be a day, month, year or time of specific date.
It always returns an integer which is a part of a date.
Syntax
DATEPART(datepart, inputdate)
datepart is the part of a date to be extracted.
inputdate is the date from which the date part is extracted.
Lets look at an example of DATEPART() function in SQL .
DECLARE @date DATETIME = '2018-03-27 18:23:45.470'; SELECTย DATEPART(year, @date) asย year, DATEPART(quarter, @date)ย as quarter, DATEPART(month, @date) as month, DATEPART(day, @date)ย as day, DATEPART(dayofyear ,@date) as dayofyear, DATEPART(Week ,@date) as week, DATEPART(hour, @date) asย hour, DATEPART(minute, @date) as minute, DATEPART(second, @date)ย as second , DATEPART(millisecond, @date) as millisecond , DATEPART(microsecond, @date) as microsecond , DATEPART(nanosecond, @date) as nanosecond
ย As you can see, It returns all the part of date accordingly.
So using a datepart() function, you can get any part of date, below given are the valid datepart list that you can use to get a part of date as per your requirement.
dayย ย ย ย ย ย ย ย ย d, dd monthย ย ย ย ย ย ย ย m, mm yearย ย ย ย ย ย ย ย yy, yyyy quarterย ย ย ย ย ย ย qq, q hourย ย ย ย ย ย ย ย ย hh minuteย ย ย ย ย ย ย mi, n secondย ย ย ย ย ย ย ย ss, s millisecondย ย ย ย ms microsecondย ย mcs nanosecondย ย ย ns weekย ย ย ย ย ย ย ย wk, ww dayofyearย ย ย ย dy, y
Recommended..
2,044 total views, 2 views today