How to get the current date without the time part
The fastest if you have to iterate over a recordset and don't have date in SQL Server 2008
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
Two different and excellent answers on StackOverflow bear this out: One, Two
Varchar conversions are one of the worst ways to do it. Of course, for one value it may not matter but it's a good habit to get into.
This way is also deterministic, say if you want to index a computed column. Even folk who write books about SQL Server get caught out with datetime conversions
This technique is also extendable.
- yesterday:
DATEADD(day, DATEDIFF(day, 0, GETDATE()), -1)
- start of month:
DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
- end of last month:
DATEADD(month, DATEDIFF(month, 0, GETDATE()), -1)
- start of next month:
DATEADD(month, DATEDIFF(month, 0, GETDATE()), 31)
Edit:
As I mentioned about determinism, varchar methods are not safe unless you use style 112.
Other answers here point out that you'd never apply this to a column. This is correct, but you may want to select a 100k rows or add a computed column or GROUP BY dateonly. Then you have to use this method.
The other answer also mentions style 110. This is not language or SET DATEFORMAT safe and fails with "british" language setting. See the ultimate guide to the datetime datatypes by Tibor Karaszi.
You have to convert it to varchar specifying a format pattern (110 in this case) then convert (or cast) back to datetime.
select getdate(), cast(convert(varchar(10), getdate(), 110) as datetime)