请问如何查询离当天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' ,这样可以吗?

解决方案 »

  1.   

    SELECT * FROM tbname WHERE appdate > day(getdate()-7)
      

  2.   

    create table #tt(id int,appdate varchar(10),pname varchar(10))insert #tt select 1 ,'2010-4-1','A1'
    insert #tt select 2 ,'2010-4-2','A2'
    insert #tt select 3 ,'2010-4-3','A3'
    insert #tt select 4 ,'2010-4-4','A4'
    insert #tt select 5 ,'2010-4-5','A5'
    insert #tt select 6 ,'2010-4-6','A6'
    insert #tt select 7 ,'2010-4-7','A7'
    insert #tt select 8 ,'2010-4-8','A8'
    insert #tt select 9 ,'2010-4-9','A9'select * from #tt where DATEDIFF(day,appdate,GETDATE())between 0 and 6         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(7 行受影响)
      

  3.   

    select * from tb where datediff(dd,appdate,getdate())<=7
      

  4.   

    getdate()-appdate between 1 and 7
      

  5.   

    select * from tbname where (getdate()-appdate)='7' ,这样可以吗?
    ---上面是可以的.但是效率不高,在列上用表达式是不推荐的
    select * from tbname where appdate between convert(varchar(10),getdate()-7,120)+' 0:00:00' and getdate()
    这样可以,如果appdate上面有索引,这样是可以利用到的.
      

  6.   

    select * from tb where appdate between getdate()-7 and getdate()-1如果appdate没有时分秒,可以这样。
      

  7.   


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

  8.   

    select * from tbname where datepart(day,getdate())-datepart(day,appdate)=7
      

  9.   

    select * from tbname where convert(char(8),appdate,112) <= 7 convert(char(8),getdate()-1,112) ,取整7天的
      

  10.   

    写错了,重来
    select * from DBNAME
     where convert(char(8),APPDATE,112) BETWEEN convert(char(8),GETDATE()-7,112) AND convert(char(8),GETDATE()-1,112) 
      

  11.   

    select * from tb where datediff(dd,appdate,getdate())<=7
      

  12.   

    select * from #tt where appdate < dateadd(day,-1,getdate()) and appdate >dateadd(day,-8,getdate())
      

  13.   

    我也不推荐在列上使用函数,这样索引就不起作用了
    最好是采用 between and方式
      

  14.   

    select * from tbname where appdate >= getdate() - 7
      

  15.   


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

  16.   

    select * from tb where datediff(dd,appdate,getdate())<=7
      

  17.   

    select * from tb where appdate > ADDDATE(getdate(),-7);
      

  18.   

    select * from tb where appdate > ADDDATE(now(),-7);
      

  19.   

    直接两个日期相关肯定不行
    应用datediff函数计算两日期相差的天数