dateTime字段和sql中的dateTime.Now之间的区别

时间:2014-07-16 06:39:06

标签: sql datetime datediff

我想为此目的创建一个查询:

从我的表[tblTempBlackList]中删除,其dateTime字段和dateTime.Now之间的差异超过240分钟。

我尝试了这个查询,但没有工作:

DELETE FROM [tblTempBlackList] 
WHERE (SELECT DATEDIFF(MINUTE, (SELECT [tblTempBlackList].BlackListDateTime 
                                FROM [tblTempBlackList] 
                                WHERE MobileNumber = '') , GETDATE()
                      )
      ) > '240'`

2 个答案:

答案 0 :(得分:2)

试试这个:

DELETE FROM tblTempBlackList
WHERE 
DATEDIFF(MINUTE, (SELECT [tblTempBlackList].BlackListDateTime FROM [tblTempBlackList] WHERE MobileNumber = ''), GETDATE()) > 240

或者,更简洁:

DELETE FROM tblTempBlackList
WHERE 
DATEDIFF(MINUTE, BlackListDateTime, GetDate()) > 240
AND MobileNumber = ''

答案 1 :(得分:0)

保持简单:

DECLARE @DeleteBefore DATETIME = DATEADD(MINUTE, -240, GETDATE())

DELETE FROM [tblTempBlackList]
WHERE
  BlackListDateTime < @DeleteBefore
  AND MobileNumber = ''