Get date 3 days ago

I have an SQL script that selects everything from the current day.

SELECT [ClientID] from [logs] where Date > CONVERT (date, SYSDATETIME()) 

Date is a DateTime type.

How to get everything in the last 3 days? I suppose I need to subtract 3 days from the result of the SYSDATETIME() function, but how?

+5
source share
6 answers
 SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, CONVERT (date, SYSDATETIME())) 
+6
source

For mysql use this:

 SELECT DATE_ADD(CURRENT_DATE, INTERVAL - 3 DAY); 
+2
source

Use BETWEEN

 SELECT ClientID FROM logs WHERE Date BETWEEN SYSDATETIME() AND SYSDATETIME() - 3 
+1
source

Use GETDATE() : Yes, it gets a date from the system!

Returns the current timestamp of the database system as a date and time value without a database time zone offset. This value is obtained from the operating system of the computer on which the instance of SQL Server is running.

Query:

 SELECT [ClientID] from [logs] where ( Date > GETDATE() - 3) 

Additional Information:

detailed getdate documentation

+1
source

Using BETWEEN is good. I also prefer the DATEADD function. But keep in mind that the SYSDATETIME function (or I would GETDATE ()) also includes a time, which will mean that the events are up to the current time, but for three days may not be turned on. You may need to convert both sides to a date instead of a date and time.

0
source
 SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, SYSDATETIME()) 
0
source

All Articles