0

此查询应该获取在特定日期创建的学生数据。但是,如果我为 @NumberOfDays 输入 15,它会返回 15 天和 14 天的数据,如果我输入 20,这一次它会带来 19 天和 20 天的数据。有人可以帮我解决我的问题吗?我不知道我做错了什么。

SELECT TOP 10 S.Id, S.Name, S.Surname, S.DateCreated
FROM STUDENT S WITH(NOLOCK)
WHERE DATEDIFF(day, CAST(S.DateCreated AS date), CAST(GETDATE() AS date)) = @NumberOfDays
4

2 回答 2

0
SELECT TOP 10 S.Id, S.Name, S.Surname, S.DateCreated
FROM STUDENT S WITH(NOLOCK)
WHERE DATEDIFF(day, CAST(S.DateCreated AS date), CAST(GETDATE() AS date)) 
Between @NumberOfDays-1 and @NumberOfDays
于 2012-06-26T11:53:23.530 回答
0

有了这个查询,它终于可以工作了!

SELECT TOP 10 S.Id, S.Name, S.Surname, S.DateCreated
FROM STUDENT S WITH(NOLOCK)
WHERE CAST(S.DateCreated AS DATE) = CAST(DATEADD(DAY, -@NumberOfDays, GETDATE()) AS DATE)
于 2012-06-28T10:13:11.023 回答