select * from table where birth_date between getdate()-7 and getdate()

解决方案 »

  1.   

    SELECT * FROM TABLE WHERE DATEDIFF(day, GETDATE(),BIRTH_DATE)>7
      

  2.   

    反了,是
    SELECT * FROM TABLE WHERE DATEDIFF(day, BIRTH_DATE,GETDATE())>7
      

  3.   

    谢谢回复。不过BIRTH_DATE和GETDATE()能直接比较吗?他们之间还有年的啊。
    一条纪录:他的birth_date值为:1971-01-10 00:00:00.000
    能和getdate()比较吗?
      

  4.   

    可以比较,函数DATEDIFF中的day表示只计算这两个日期的“日”间隔
      

  5.   

    SELECT * FROM TABLE WHERE DATEDIFF(day, BIRTH_DATE,GETDATE())>7
    这个肯定不行的。
      

  6.   

    上面的都没有考虑到年这个问题。楼主试试这个,可以实现。Select * from [table] 
    Where DateDiff(dd,Convert(DateTime,Convert(Varchar(6),birth_date,110)+'05'),GetDate())<7 And DateDiff(dd,Convert(DateTime,Convert(Varchar(6),birth_date,110)+'05'),GetDate())>=0我看还能不能简化点。
      

  7.   

    SELECT * FROM 表 WHERE DATEDIFF(day, 字段,GETDATE())>7可以的
      

  8.   

    SELECT * FROM TABLE WHERE DATEDIFF(day, birth_date,GETDATE())>7
    這個行。
    沒有問題
    其中datediff()這個函數的運用了
      

  9.   


    --建立测试环境
    Create table [table]
    (ID Int,
     birth_date DateTime)
    --插入数据
    Insert [table] Values(1,'1981/12/26')
    Insert [table] Values(2,'1977/02/26')
    Insert [table] Values(3,'1978/01/05')
    Insert [table] Values(4,'1979/01/01')
    Insert [table] Values(5,'1980/12/27')
    Insert [table] Values(6,'1980/12/25')
    GO
    --测试
    Declare  @I Int
    Declare  @Date DateTime
    Set @I=8 --天数
    Set @Date='2005/01/02' --当前日期
    Select * from [table] Where 
    (DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)<=@I 
     And DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)>=0) 
    Or (DateDiff(dd,Convert(Varchar(6),birth_date,110)+'05',@Date)<-(365-@I))
    --删除测试环境
    Drop table [table]
    --结果
    /*
    ID birth_date
    1 1981-12-26 00:00:00.000
    4 1979-01-01 00:00:00.000
    5 1980-12-27 00:00:00.000
    */
      

  10.   

    回复人: Hopewell_Go(好的在后頭﹗希望更好﹗﹗) ( ) 信誉:100  2005-04-29 17:32:00  得分: 0  
     
     
       SELECT * FROM TABLE WHERE DATEDIFF(day, birth_date,GETDATE())>7
    這個行。
    沒有問題
    其中datediff()這個函數的運用了
      
     
    ----------------------------------------
    这样不行的。就我上面的数据。查询的结果是
    ID birth_date
    1 1981-12-26 00:00:00.000
    2 1977-02-26 00:00:00.000
    3 1978-01-05 00:00:00.000
    4 1979-01-01 00:00:00.000
    5 1980-12-27 00:00:00.000
    6 1980-12-25 00:00:00.000都说了这样写,没有考虑好年份的。
      

  11.   

    select billdate
    from t
    where datediff(d,dateadd(y,datediff(y,getdate(),billdate),billdate),getdate())<7
      

  12.   

    paoluo(一天到晚游泳的鱼) ( ) 信誉:100  2005-04-29 18:04:00  得分: 0  
     
     
       回复人: Hopewell_Go(好的在后頭﹗希望更好﹗﹗) ( ) 信誉:100  2005-04-29 17:32:00  得分: 0  
     
     
       SELECT * FROM TABLE WHERE DATEDIFF(day, birth_date,GETDATE())>7
    這個行。
    沒有問題
    其中datediff()這個函數的運用了
      
     
    ----------------------------------------
    这样不行的。就我上面的数据。查询的结果是
    ID birth_date
    1 1981-12-26 00:00:00.000
    2 1977-02-26 00:00:00.000
    3 1978-01-05 00:00:00.000
    4 1979-01-01 00:00:00.000
    5 1980-12-27 00:00:00.000
    6 1980-12-25 00:00:00.000都说了这样写,没有考虑好年份的。
      
     
    ----昏,這樣來計算得話,逐一-365天 ,並要考慮366天得
      

  13.   

    Create table [table]
    (ID Int,
     birth_date DateTime)
    --插入数据
    Insert [table] Values(1,'1981/12/26')
    Insert [table] Values(2,'1977/02/26')
    Insert [table] Values(3,'1978/01/05')
    Insert [table] Values(4,'1979/01/01')
    Insert [table] Values(5,'1980/12/27')
    Insert [table] Values(6,'1980/12/25')
    GO
    declare @date datetime
    set @date='2005/1/2'
    select * from [table] where 
    (case when convert(varchar(5),@date-7,1)>convert(varchar(5),@date,1) then 
    (case when convert(varchar(5),birth_date,1)<convert(varchar(5),@date-7,1) and  convert(varchar(5),birth_date,1)>convert(varchar(5),@date,1) then 0 else 1 end)
    else (case when convert(varchar(5),birth_date,1) between convert(varchar(5),@date-7,1) and convert(varchar(5),@date,1)
    then 1 else 0  end)
    end)=1drop table [table]
      

  14.   

    我的思路是:如果7天前的日期(月日)大于这一天的(月日),则  生日大于这一天且小于7天前的日期 都不满足,其它日期都满足
    如果7天前的日期(月日)小于这一天的(月日),则用between ... and ...
      

  15.   

    select datediff(day,'2004/12/29','2005/01/05')
    Create table [table]
    (ID int,
     birth_date DateTime)
    --插入数据
    Insert [table] Values(1,'1981/12/26')
    Insert [table] Values(2,'1977/02/26')
    Insert [table] Values(3,'1978/01/05')
    Insert [table] Values(4,'1979/01/01')
    Insert [table] Values(5,'1980/12/27')
    Insert [table] Values(6,'1980/12/25')
    SELECT * FROM [TABLE] WHERE DATEDIFF(day, birth_date,'1981/01/01')<71 1981-12-26 00:00:00.000
    5 1980-12-27 00:00:00.000
      

  16.   

    再加上年份限制应该可以了.
    SELECT * FROM ls_TABLE5 WHERE DATEDIFF(day, birth_date,GETDATE())>7 and year(getdate())-year(birth_date)<=1
      

  17.   

    Select * from [table] 
    Where DateDiff(dd,Convert(DateTime,Convert(Varchar(6),dateadd(dd,7,birth_date),110)+'05'),dateadd(dd,7,GetDate())<7 And
     DateDiff(dd,Convert(DateTime,Convert(Varchar(6),dateadd(dd,7,birth_date),110)+'05'),dateadd(dd,7,GetDate())>=0