请问如何查询离当天7天的记录

mingjunwang 2010-04-07 10:24:23
请问如何查询离当天7天人记录
如:
id appdate pname
1 2010-4-1 A1
2 2010-4-2 A2
3 2010-4-3 A3
4 2010-4-4 A4
5 2010-4-5 A5
6 2010-4-6 A6
7 2010-4-7 A7
8 2010-4-8 A8
9 2010-4-9 A9
.....
若今天是8号,那就要把id为1到7的记录查出。用select * from tbname where (getdate()-appdate)='7' ,这样可以吗?
...全文
245 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
ablihui3 2010-04-14
  • 打赏
  • 举报
回复
select * from tb where appdate > ADDDATE(now(),-7);
ablihui3 2010-04-14
  • 打赏
  • 举报
回复
select * from tb where appdate > ADDDATE(getdate(),-7);
zhuoruling 2010-04-14
  • 打赏
  • 举报
回复
差不多,顶个
baobeituping 2010-04-14
  • 打赏
  • 举报
回复
select * from tb where datediff(dd,appdate,getdate())<=7
cxmcxm 2010-04-14
  • 打赏
  • 举报
回复
直接两个日期相关肯定不行
应用datediff函数计算两日期相差的天数
tanshi 2010-04-11
  • 打赏
  • 举报
回复

SELECT * FROM table
WHERE appdate
between CONVERT(varchar,year(getDate()))+'-'+CONVERT(varchar,month(getDate()))+'-'+CONVERT(varchar,day(getDate())-7)
and CONVERT(varchar,year(getDate()))+'-'+CONVERT(varchar,month(getDate()))+'-'+CONVERT(varchar,day(getDate()))
Leshami 2010-04-10
  • 打赏
  • 举报
回复
select * from tbname where appdate >= getdate() - 7
beifengchuiqi 2010-04-10
  • 打赏
  • 举报
回复
上面都说了。。
ontrackfor19888 2010-04-10
  • 打赏
  • 举报
回复
学习了
!!!
ycagri 2010-04-10
  • 打赏
  • 举报
回复
我也不推荐在列上使用函数,这样索引就不起作用了
最好是采用 between and方式
cnfuyou 2010-04-10
  • 打赏
  • 举报
回复
select * from #tt where appdate < dateadd(day,-1,getdate()) and appdate >dateadd(day,-8,getdate())
QQHRSWB 2010-04-09
  • 打赏
  • 举报
回复
写错了,重来
select * from DBNAME
where convert(char(8),APPDATE,112) BETWEEN convert(char(8),GETDATE()-7,112) AND convert(char(8),GETDATE()-1,112)
wqmgxj 2010-04-09
  • 打赏
  • 举报
回复
select * from tb where datediff(dd,appdate,getdate())<=7
lrjt1980 2010-04-09
  • 打赏
  • 举报
回复
用特殊日期函数。
sxiaohui8709253 2010-04-09
  • 打赏
  • 举报
回复
学习,,,
  • 打赏
  • 举报
回复
select * from tbname where datepart(day,getdate())-datepart(day,appdate)=7
hbjlwhl 2010-04-08
  • 打赏
  • 举报
回复
一定可以的。但是你写的不对。
fan7421436 2010-04-08
  • 打赏
  • 举报
回复
学习··
QQHRSWB 2010-04-08
  • 打赏
  • 举报
回复
select * from tbname where convert(char(8),appdate,112) <= 7 convert(char(8),getdate()-1,112) ,取整7天的
htl258_Tony 2010-04-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 htl258 的回复:]
SQL code
select * from tb where appdate between getdate()-7 and getdate()-1


如果appdate没有时分秒,可以这样。
[/Quote]

select * from tb where appdate between convert(varchar,getdate()-7,23) and convert(varchar,getdate()-1,23)
7楼修改,代码不够严密。
加载更多回复(10)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧