SQL Server DATEPART()Function


SQL DatesSQL Server Date Functions

Definition and Usage

The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

Syntax

DATEPART(datepart,date)

The date parameter is a valid date expression. The datepart parameter can be one of the following values:

datepart Abbreviation
year yy, yyyy
Quarter qq, q
month mm, m
Day of year dy, y
day dd, d
week wk, ww
Week dw, w
Hour hh
Minute mi, n
second ss, s
Millisecond ms
Microsecond mcs
Nanosecond ns


Example

Suppose we have the following "Orders" table:

OrderId ProductName OrderDate
1 Jarlsberg Cheese 2008-11-11 13:23:44.657

The following is the SELECT statement:

SELECT DATEPART(yyyy,OrderDate) AS OrderYear,
DATEPART(mm,OrderDate) AS OrderMonth,
DATEPART(dd,OrderDate) AS OrderDay,
FROM Orders
WHERE OrderId=1

The result is shown as follows:

OrderYear OrderMonth OrderDay
2008 11 11


SQL DatesSQL Server Date Functions Other Extensions