SQL-选择前一天并排除今天

时间:2014-04-21 21:48:53

标签: sql-server-2008 tsql

我需要包含昨天注册的所有记录,但不包括今天已注册的所有记录。我不确定这个查询是否给了我正确的记录数。请帮忙

select id, lastmodifieddate  from stu_tbl
where last_modified_date > DATEADD(d, - 1, GETDATE())) 
and _last_modified_date <>  GETDATE())

3 个答案:

答案 0 :(得分:0)

试试这个:

select id, last_modifieddate  from stu_tbl
where last_modified_date >= DATEADD(d, -1, cast(GETDATE() as date)) 
  and last_modified_date < cast(GETDATE() as date) 

答案 1 :(得分:0)

这是SQL Server 2008+解决方案:

select id, lastmodifieddate  
from stu_tbl
where last_modified_date >= cast(dateadd(day, -1, GetDate()) as Date)
and last_modified_date < cast(GetDate() as Date)

答案 2 :(得分:0)

试试这个

declare @Dfrom  datetime
declare @Dto    datetime
declare @Dyesterday datetime

--Get date before current date
set @Dyesterday = dateadd(day, -1, getdate())

--Set date from to 12am
set @Dfrom = CONVERT(varchar(20), @Dyesterday, 101)

--set date to to 11:59:59PM
set @Dto = CONVERT(varchar(20), @Dyesterday, 101) + ' 23:59:59'

--if date and time is required
select  id, lastmodifieddate
from    stu_tbl
where   last_modified_date between @Dfrom and @Dto

OR

--if date only and time is not required
select  id, lastmodifieddate
from    stu_tbl
where   last_modified_date = @Dfrom