DATEPART Example SQL Server Standard Datetime Function

DATEPART is a SQL Server built in function that returns a specific part of the date/time value. Such as day, month, year, hour, minute etc.

SQL Server DatePart Function Syntax:


where datepart (part of a date) can have the following:

datepart Abbreviation
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns

SQL Server DatePart Function Example SQL:
Let we have a table like below:

00000001 Shawpnendu Bikash Maloroy 1979-01-01 10:05

Now we want day, month, year & quarter value from DOB column. To achieve this we have to write the following sql by using SQL Server builtin function DATEPART():

SELECT DATEPART(yyyy,DOB) [Year], DATEPART(mm,DOB) [Month], DATEPART(dd,DOB) [Day], DATEPART(dayofyear,DOB) [Day of Year] FROM Developers

The output will be:

Year Month Day Day of Year
1979 1 1 1
Posted in Database, SQL Server

Leave a Reply

Your email address will not be published. Required fields are marked *