SQL Server DATEPART () function
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
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 Server Date Functions