我需要編寫一個SQL Select語句來選擇DateCreated在過去4週週末所有的記錄。我知道我可以通過硬編碼選擇,如下所示。但我想知道是否有更簡單的方法使用T-sql日期函數。SQL - 僅從週末選擇記錄
SELECT * FROM audit_table
WHERE DateCreated BETWEEN '07-31-2010 00:00:00' AND '08-01-2010 23:59:59'
OR DateCreated BETWEEN '07-24-2010 00:00:00' AND '07-25-2010 23:59:59'
OR DateCreated BETWEEN '07-17-2010 00:00:00' AND '07-18-2010 23:59:59'
OR DateCreated BETWEEN '07-10-2010 00:00:00' AND '07-11-2010 23:59:59'
請注意,您需要提防你的SQL Server安裝的想法是第一天本週 - 查看'@@ DATEFIRST'的文檔 - http://msdn.microsoft.com/en-us/library/ms187766.aspx – 2010-08-06 15:49:27
'DateCreated> = cast(floor(cast(DATEADD(WK,-4, GETDATE())作爲float)))date :)):) – dotjoe 2010-08-06 16:47:15
而在SQL 2008上,你可以做CONVERT( DATE,DATEADD(WK,-4,GETDATE())) – SQLMenace 2010-08-06 16:59:34