How to get only date when using MSSQL GetDate ()?

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 
+50
date sql datetime sql-server
Jan 26 '11 at 20:59
source share
7 answers

Slight bias to SQL Server

  • Best approach to remove datetime time part in SQL Server
  • The most efficient way in SQL Server to get dates from date + time?

Summary

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

SQL Server 2008 is of type date . Therefore just use

 CAST(GETDATE() AS DATE) 

Edit: add one day, compare with day to "zero"

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

From cyberkiwi:

An alternative that does not include 2 functions is (+1 may be in brackets or on our sides).

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

DateDiff returns a number, but for all purposes it will work as the date where you intend to use this expression, except for converting it directly to VARCHAR - in this case you would use the CONVERT approach directly in GETDATE (), for example,

 convert(varchar, GETDATE() +1, 102) 
+63
Jan 26 '11 at 9:15
source share

For SQL Server 2008, the best and easiest way to index is

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

For previous versions of SQL Server, date math will work faster than converting to varchar. Even conversion to varchar may give an incorrect result due to regional settings.

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

Note: there is no need to wrap a DATEDIFF another DATEADD

+9
Jan 26 2018-11-21T00:
source share

This database is specific. You did not indicate which database engine you are using.

eg. in PostgreSQL, you throw (myvalue as date).

+7
Jan 26 2018-11-21T00:
source share
 SELECT CONVERT(DATETIME, CONVERT(varchar(10), GETDATE(), 101)) 
+4
Jan 26 2018-11-21T00:
source share

you can use

 DELETE from Table WHERE Date > CONVERT(VARCHAR, GETDATE(), 101); 
0
Feb 07 '13 at 10:28
source share
 CONVERT(varchar,GETDATE(),102) 
-one
Jan 26 2018-11-21T00:
source share
-one
Jan 26 2018-11-21T00:
source share



All Articles