SQL Server數據庫DATEADD的語法介紹及使用實例
之前我們介紹了:SQL Server數據庫ISNULL函數的應用實例,本文我們介紹一下DATEADD函數的語法介紹及使用實例,接下來就讓我們一起來了解一下這部分內容。
1.語法
DATEADD (datepart , number , date )
2.參數
datepart是與integernumber相加的date部分。下表列出了所有有效的datepart參數。用戶定義的變量等效項是無效的。
datepart |
縮寫 |
---|---|
year |
yy ,yyyy |
quarter |
qq ,q |
month |
mm ,m |
dayofyear |
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 |
number是一個表達式,可以解析為與date的datepart相加的int。用戶定義的變量是有效的。如果您指定一個帶小數的值,則將小數截去且不進行舍入。
date是一個表達式,可以解析為 time、date、smalldatetime、datetime、datetime2 或 datetimeoffset 值。date 可以是表達式、列表達式、用戶定義的變量或字符串文字。如果表達式是字符串文字,則它必須解析為一個 datetime 值。為避免不確定性,請使用四位數年份。
3.例子:返回最近3個月的訂單。有考慮具體的日期。
- select * from ordersT where AddDate>DATEADD(M,-3,GETDATE()) order by AddDate
返回前3個月的訂單,若不考慮日期,則從1號開始算起。 代碼如下:
- select * from OrdersT where DATEDIFF(m,AddDate,getdate())<=3 order by AddDate
4.顯示今天的訂單
- select * from ordersT where AddDate>CONVERT(varchar,getdate(),112) order by AddDate
關于SQL Server數據庫DATEADD的語法介紹及使用實例就介紹到這里了,希望本次的介紹能夠對您有所收獲!
【編輯推薦】