Latest web development tutorials

SQL Server DATEPART () function

SQL Dates SQL Server Date Functions

Definition and Usage

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

grammar

DATEPART(datepart,date)

date date expression argument is legitimate. datepart parameter can have the following values:

datepart 缩写
yy, yyyy
季度 qq, q
mm, m
年中的日 dy, y
dd, d
wk, ww
星期 dw, w
小时 hh
分钟 mi, n
ss, s
毫秒 ms
微妙 mcs
纳秒 ns


Examples

Suppose we have the following "Orders" table:

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

Here 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 results are as follows:

OrderYear OrderMonth OrderDay
2008 11 11


SQL Dates SQL Server Date Functions