0

我想列出在特定天数内添加到表格中的学生。

@NumberOfDays INT = 15 -- When I list them the added subtraction result must match this result

SELECT S.Name, S.Surname, S.DateAdded
FROM STUDENT S
WHERE (SELECT CAST(GETDATE()-S.DateAdded) = @NumberOfDays)

但这只是行不通。谁能帮帮我?

4

5 回答 5

5

DATEADD增量将扣除;

where S.DateAdded >= dateadd(day, -@NumberOfDays, getdate())

对于过去 15 天内添加的任何内容。

于 2012-04-16T13:04:11.260 回答
3

您应该使用函数 DATEDIFF (Transact-SQL),以下应该可以工作:

DECLARE @NumberOfDays INT = 15 -- When I list them the added subtraction result must match this result

SELECT S.Name, S.Surname, S.DateAdded
FROM STUDENT S
WHERE DATEDIFF(day, S.DateAdded, GETDATE()) = @NumberOfDays

有关此功能的更多信息:

http://msdn.microsoft.com/en-us/library/ms189794.aspx

于 2012-04-16T13:05:21.103 回答
3

您需要以DATEADD这种方式使用该功能:

declare @numberofdays int
set @numberofdays = 15

SELECT S.Name, S.Surname, S.DateAdded
FROM STUDENT S
WHERE S.DateAdded >= dateadd(day, -@NumberOfDays, getdate())

这是一个带有它的sqlfiddle

于 2012-04-16T13:05:52.640 回答
0
SELECT S.Name, S.Surname, S.DateAdded 
FROM STUDENT S 
WHERE (SELECT months_between(GETDATE(), S.DateAdded) = @NumberOfDays) 
于 2012-04-16T13:25:19.513 回答
0

这个怎么样。

您可以在 sql 中使用datediff函数来获取以天为单位的日期差异。

DECLARE @numberofdays as int
set @numberofdays = 15

SELECT S.Name, S.Surname, S.DateAdded
from STUDENT S
where datediff(dd,S.DateAdded,getdate())= @numberofdays;
于 2012-04-16T13:07:55.150 回答