2014-04-21 117 views
0

我需要包含昨天注册的所有记录,但不包括今天已经注册的任何记录。我不确定这个查询是否给了我正确的记录数。请帮助SQL-select前一天,今天排除

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

请参阅下面的答案。 – Jade

回答

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) 
+0

谢谢,但最后一行'和last_modified_date user3502587

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) 
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