Bookmark and Share

SQL - Datepart

DATEPART() is a SQL function used to extract all kinds of date information from timestamps, and it is a function that is unique to Microsoft's SQL Server Application.

Advertise on

SQL Datepart:

USE mydatabase;

SELECT DATEPART(year, '2007-06-01') AS "Year";

SQL Results:


DATEPART() requires 2 parameters separated by a comma (,). The first parameter specifies what type of date data will be extracted, and the second parameter is a timestamp value.

SQL Datepart:

USE mydatabase;

SELECT DATEPART(year, '2007-06-01') AS "Year",
    DATEPART(month, '2007-06-01') AS "Month",
    DATEPART(day, '2007-06-01') AS "Day",
    DATEPART(dayofyear, '2007-06-01') AS "DayofYear",
    DATEPART(weekday, '2007-06-01') AS "Weekday";

SQL Results:


Datepart Abbreviation Chart:

yearyy, yyyyDATEPART(yy, getdate())
quarterqq, qDATEPART(qq, getdate())
monthmm, mDATEPART(mm, getdate())
dayofyeardy, yDATEPART(dy, getdate())
daydd, dDATEPART(dd, getdate())
weekwk, wwDATEPART(wk, getdate())
weekdaydwDATEPART(dw, getdate())
hourhhDATEPART(hh, getdate())
minutemiDATEPART(mi, getdate())
secondssDATEPART(ss, getdate())
millisecondmsDATEPART(ms, getdate())
Bookmark and Share

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials