Skip to content
Home » SQL DATEPART()

SQL DATEPART()

datepart

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

datepart

 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

Also Read..

SQL Server DateName()

SQL Server DateFromParts()

 




Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SQL BI Tutorials

Subscribe now to keep reading and get access to the full archive.

Continue reading