选择昨天的日期在哪里条件

时间:2017-01-30 05:22:48

标签: sql sql-server

我使用以下条件,其中condition为0以选择今天日期的值

where (DateDiff(d, FilteredPhoneCall.createdon, GETDATE()) = 0 
      or DateDiff(d, FilteredPhoneCall.modifiedon, GETDATE()) = 0)

但是我需要在where条件中选择昨天的值。如果我使用-1而不是0。它没有返回昨天的日期值。

where (DateDiff(d, FilteredPhoneCall.createdon, GETDATE()) = -1 
      or DateDiff(d, FilteredPhoneCall.modifiedon, GETDATE()) = -1)

4 个答案:

答案 0 :(得分:2)

SELECT
* 
FROM yourTable
WHERE 
(FilteredPhoneCall.createdon >= dateadd(day,datediff(day,1,GETDATE()),0)
    AND FilteredPhoneCall.createdon < dateadd(day,datediff(day,0,GETDATE()),0)) 
OR
(FilteredPhoneCall.modifiedon >= dateadd(day,datediff(day,1,GETDATE()),0)
    AND FilteredPhoneCall.modifiedon < dateadd(day,datediff(day,0,GETDATE()),0))

答案 1 :(得分:1)

尝试中间条件;

WHERE FilteredPhoneCall.createdon between DATEADD(DAY, -1, GETDATE()) and GETDATE()

答案 2 :(得分:1)

使用此:

 Where FilteredPhoneCall.createdon >= DATEADD(day,-1, GETDATE())

答案 3 :(得分:0)

试试这个: -

WHERE CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) =CAST(FilteredPhoneCall.createdon AS DATE)