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.
日期是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天,但是如何?
6 个解决方案
#1
8
SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, CONVERT (date, SYSDATETIME()))
#2
2
For mysql use this:
对于mysql使用此:
SELECT DATE_ADD(CURRENT_DATE, INTERVAL - 3 DAY);
#3
2
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 computer on 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详细文档
#4
1
Use BETWEEN
使用BETWEEN
SELECT ClientID
FROM logs
WHERE Date BETWEEN SYSDATETIME() AND SYSDATETIME() - 3
#5
0
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())还包括时间,这意味着可能不包括当前时间之前但在三天内的事件。您可能必须将双方转换为日期而不是日期时间。
#6
0
SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, SYSDATETIME())
#1
8
SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, CONVERT (date, SYSDATETIME()))
#2
2
For mysql use this:
对于mysql使用此:
SELECT DATE_ADD(CURRENT_DATE, INTERVAL - 3 DAY);
#3
2
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 computer on 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详细文档
#4
1
Use BETWEEN
使用BETWEEN
SELECT ClientID
FROM logs
WHERE Date BETWEEN SYSDATETIME() AND SYSDATETIME() - 3
#5
0
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())还包括时间,这意味着可能不包括当前时间之前但在三天内的事件。您可能必须将双方转换为日期而不是日期时间。
#6
0
SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, SYSDATETIME())