SQL Server DATEADD () function
Definition and Usage
DATEADD () function adds to a date or subtract a specified time interval.
grammar
DATEADD(datepart,number,date)
date date expression argument is legitimate. number is the number of intervals you want to add; for a time in the future, this number is a positive number, for the last time, this number is negative.
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 |
Now, we want to "OrderDate" add 45 days, so you can find the date of payment.
We use the following SELECT statement:
SELECT OrderId,DATEADD(day,45,OrderDate) AS OrderPayDate
FROM Orders
FROM Orders
result:
OrderId | OrderPayDate |
---|---|
1 | 2008-12-26 13:23:44.657 |
SQL Server Date Functions