Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 521
sooprise Avatar asked Jan 26 '11 20:01

sooprise


People also ask

How do I extract just the day from a date in sql?

If you use SQL Server, you can use the DAY() or DATEPART() function instead to extract the day of the month from a date. Besides providing the EXTRACT() function, MySQL supports the DAY() function to return the day of the month from a date.


1 Answers

Slight bias to SQL Server

  • Best approach to remove time part of datetime in SQL Server
  • Most efficient way in SQL Server to get date from date+time?

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) 
like image 196
gbn Avatar answered Oct 15 '22 04:10

gbn