How do I get just the date when using MSSQL GetDate()?

SqlSql ServerTsqlDateDatetime

Sql Problem Overview


DELETE from Table WHERE Date > GETDATE();

GETDATE() includes time. Instead of getting

2011-01-26 14:58:21.637

How can I get:

2011-01-26 00:00:00.000

Sql Solutions


Solution 1 - Sql

Slight bias to SQL Server

Summary

DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)

SQL Server 2008 has date type though. So just use

CAST(GETDATE() AS DATE)

Edit: To add one day, compare to the day before "zero"

DATEADD(day, DATEDIFF(day, -1, GETDATE()), 0)

From cyberkiwi:

An alternative that does not involve 2 functions is (the +1 can be in or ourside the brackets).

DATEDIFF(DAY, 0, GETDATE() +1)

DateDiff returns a number but for all purposes this will work as a date wherever you intend to use this expression, except converting it to VARCHAR directly - in which case you would have used the CONVERT approach directly on GETDATE(), e.g.

convert(varchar, GETDATE() +1, 102)

Solution 2 - Sql

For SQL Server 2008, the best and index friendly way is

DELETE from Table WHERE Date > CAST(GETDATE() as DATE);

For prior SQL Server versions, date maths will work faster than a convert to varchar. Even converting to varchar can give you the wrong result, because of regional settings.

DELETE from Table WHERE Date > DATEDIFF(d, 0, GETDATE());

Note: it is unnecessary to wrap the DATEDIFF with another DATEADD

Solution 3 - Sql

It's database specific. You haven't specified what database engine you are using.

e.g. in PostgreSQL you do cast(myvalue as date).

Solution 4 - Sql

SELECT CONVERT(DATETIME, CONVERT(varchar(10), GETDATE(), 101))

Solution 5 - Sql

You can use

DELETE from Table WHERE Date > CONVERT(VARCHAR, GETDATE(), 101);

Solution 6 - Sql

CONVERT(varchar,GETDATE(),102)

Solution 7 - Sql

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsoopriseView Question on Stackoverflow
Solution 1 - SqlgbnView Answer on Stackoverflow
Solution 2 - SqlRichardTheKiwiView Answer on Stackoverflow
Solution 3 - SqlFolksLordView Answer on Stackoverflow
Solution 4 - SqlJohnOpincarView Answer on Stackoverflow
Solution 5 - SqlShafeeq KoorimannilView Answer on Stackoverflow
Solution 6 - SqlSaggioView Answer on Stackoverflow
Solution 7 - Sqlxx77aBsView Answer on Stackoverflow