SQL Server DATEPART() Function


SQL Dates SQL 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)

Where date is a valid date expression and datepart can be one of the following:

datepartAbbreviation
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
weekdaydw, w
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns

Example

Assume we have the following "Orders" table:

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

The following SELECT statement:

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

will result in this:

OrderYearOrderMonthOrderDay
20141111

SQL Dates SQL Server Date Functions

SHARE THIS PAGE

COLOR PICKER

colorpicker





Search w3schools.com:

Translate w3schools.com: