我想列出在特定天数内添加到表格中的学生。
@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)
但这只是行不通。谁能帮帮我?
DATEADD
负增量将扣除;
where S.DateAdded >= dateadd(day, -@NumberOfDays, getdate())
对于过去 15 天内添加的任何内容。
您应该使用函数 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
有关此功能的更多信息:
SELECT S.Name, S.Surname, S.DateAdded
FROM STUDENT S
WHERE (SELECT months_between(GETDATE(), S.DateAdded) = @NumberOfDays)
这个怎么样。
您可以在 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;