您的訪問代碼
Where Field = Date()
Sql Server的
Where Field = CAST(GETDATE() AS DATE)
功能GETDATE()獲取當前日期時間。
今天和過去的7天
Where Field BETWEEN CAST(DATEADD(DAY, -7, GETDATE()) AS DATE)
AND CAST(GETDATE() AS DATE)
之間的日期
我已經使用CAST()
功能的原因是因爲GETDATE()
返回當前日期時間是這樣的2014-02-09 22:09:53.067
現在如果你正在檢查值,其中場有今天的日期,它將只有值where Field = '2014-02-09 22:09:53.067'
不是記錄,其中日期2014-02-09
它也將時間考慮在那裏因爲我使用了CAST函數擺脫GETDATE()函數中的時間部分。
SELECT GETDATE() Returns '2014-02-09 22:09:53.067'
SELECT CAST(GETDATE() AS DATE) Returns '2014-02-09'
我有固定的問題與您的查詢有相當多的人:SI建議看書在線學習SQL Server的正確synatx,請參閱固定的查詢低於
SELECT TransactionTotals.[Date]
, TransactionTotals.EntryID
, TransactionItems.ItemID
, TransactionItems.ClientID
, [FirstName] + ' ' + [LastName] AS Name
, TransactionItems.[Service]
, TransactionItems.Therapist
, TransactionItems.GiftCertificate
, TransactionItems.Charge
, TransactionItems.Gratuity
, TransactionTotals.Paid
, TransactionItems.Comments
FROM CLIENTS INNER JOIN TransactionItems
ON CLIENTS.ClientID = TransactionItems.ClientID
INNER JOIN TransactionTotals
ON TransactionTotals.EntryID = TransactionItems.EntryID
WHERE TransactionTotals.[Date] = CAST(GETDATE() AS DATE)
ORDER BY TransactionTotals.EntryID DESC;
BETWEEN參數必須從最小到最大列出。 [不是說你應該使用BETWEEN進行範圍查詢](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/what-do-between-and-the-devil-have-in- common.aspx)。 –