I have a field in a table Event.EventDate and it’s of the data type DATE rather than DATETIME and then I have a view that has the following WHERE clause:
WHERE e.EventDate >= CAST(CONVERT(VARCHAR(MAX), GETDATE(), 101) AS DATETIME)
As you can see, I’m just trying to get all events >= today’s date. The above code works, but it’s ugly. I tried this …
WHERE e.EventDate >= CONVERT(VARCHAR(MAX), GETDATE(), 101)
… and this …
WHERE e.EventDate >= CONVERT(DATETIME, GETDATE(), 101)
… but those didn’t work, they gave me every event > today’s date. However, even if the above worked, it’s still ugly.
Isn’t there a better way?
Try:
WHERE e.EventDate >= cast(getdate() as date)To cast getdate() into a date time. It’s a clean way in SQL Server 2008 and up to strip out the time portion of a datetime type.