I am trying to execute this query against SQL Server 2008:
SELECT Count(*) FROM tblSharesSentRequests WHERE [SentDate] = getdate()
This returns 0 rows, even though I have 1 row with a SentDate set to “2012-7-13”
This SQL statement works fine:
SELECT Count(*) FROM tblSharesSentRequests WHERE [SentDate] = '2012-7-13'
it returns 1 row.
Why doesn’t it work when I use getdate()?
I hope
SentDateis adatetimetype variable, is it?If so try to strip the time from date time:
SELECT DATEADD(d, DATEDIFF(d, 0, GetDate()), 0)So your query becomes:
SELECT Count(*) FROM tblSharesSentRequests WHERE [SentDate] = DATEADD(d, DATEDIFF(d, 0, getdate()), 0)If you are using 2008 you can do as the comment states:
CAST(GetDate() as date).A simpler way express it is:
The type ended up being just a
Dateand not adatetime. The answer to this was to use the following:WHERE SentDate = CONVERT(Date, GetDate())