如何显示在10分钟内创建的记录

时间:2014-07-15 15:42:42

标签: sql sql-server-2008 sql-server-2008-r2

如何返回10分钟内在同一天创建的记录?我想显示所有带有创建日期smalldatetime的商品。

我试过这个,但它不起作用:

SELECT NAME,
       TITLE,
       CREATED
FROM   TABLE
WHERE  CREATEDBY = 'Billy'
GROUP  BY NAME,
          TITLE,
          CREATED,
          Datepart(YEAR, CREATED),
          Datepart(MONTH, CREATED),
          Datepart(DAY, CREATED),
          Datepart(HOUR, CREATED),
          ( Datepart(MINUTE, CREATED) / 10 ) 

1 个答案:

答案 0 :(得分:2)

根据我的理解。请尝试以下代码:

SELECT NAME,
       TITLE,
       CREATED
FROM   TABLE
WHERE CreatedBy  = 'Billy'  
AND CREATED BETWEEN DATEADD(MINUTE,-10,GETDATE()) AND GETDATE();