日期:2014-05-18  浏览次数:20446 次

如何选出今天的记录,昨天的记录,这周的记录?
假如表就两个字段
记录名称   日期


------解决方案--------------------
select *, '今天 ' rec from [Table] where datediff(day,日期,getdate())=0
Union All
select *, '昨天 ' rec from [Table] where datediff(day,日期,getdate())=1
Union All
select *, '本周 ' rec from [Table] where datediff(wk,日期,getdate())=0

------解决方案--------------------

select * from 表 where convert(char(10),日期,120)=convert(char(10),getdate(),120)
select * from 表 where convert(char(10),日期,120)= convert(char(10),dateadd(day,-1,getdate()),120)
select * from 表 where datepart(week,日期)=datepart(week,getdate())