首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么选出今天的记录,昨天的记录,这周的记录

2012-03-11 
如何选出今天的记录,昨天的记录,这周的记录?假如表就两个字段记录名称日期 [解决办法]select *, 今天rec f

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


[解决办法]
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())

热点排行