SQL Server DATEPART() Function
Definition and Usage
The DATEPART() function is used to return a specific part of a date/time, such as year, month, day, hour, minute, etc.
Syntax
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 the year | dy, y |
| Day | dd, d |
| Week | wk, ww |
| Weekday | 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 |
Here is the SELECT statement:
The result is shown below:
| OrderYear | OrderMonth | OrderDay |
|---|---|---|
| 2008 | 11 | 11 |