SQL 获取 3 天前的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31852062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:53:37  来源:igfitidea点击:

Get date of 3 days ago

sqlsql-server

提问by vico

I have SQL script that selects everything from current day.

我有 SQL 脚本,可以选择当天的所有内容。

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

Date is type of DateTime.

日期是日期时间的类型。

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

如何在过去 3 天内获得所有内容?我想我需要从函数SYSDATETIME()结果中减去 3 天,但是如何呢?

回答by Backs

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

回答by Tharif

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

使用GETDATE():是的,它从系统获取日期!

Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computeron which the instance of SQL Server is running.

将当前数据库系统时间戳作为不带数据库时区偏移量的日期时间值返回。此值源自运行 SQL Server 实例的计算机的操作系统

Query:

询问:

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

More Reference:

更多参考:

GETDATE Detailed Documentation

GETDATE 详细文档

回答by anirban karak

For mysql use this:

对于 mysql 使用这个:

SELECT DATE_ADD(CURRENT_DATE, INTERVAL - 3 DAY);

回答by Matt

Use BETWEEN

BETWEEN

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

回答by Tom

Using BETWEEN is nice. I also prefer the DATEADD function. But be aware of the fact that the SYSDATETIME function (or I would us GETDATE()) also includes the time which would mean that events before the current time but within the three day period may not be included. You may have to convert both sides to a date instead of datetime.

使用 BETWEEN 很好。我也更喜欢 DATEADD 函数。但请注意,SYSDATETIME 函数(或者我会是 GETDATE())也包括时间,这意味着可能不包括当前时间之前但在三天内的事件。您可能必须将双方转换为日期而不是日期时间。

回答by Madhivanan

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