select * from 员工表 where DATEDIFF(day,Birthday,'2004-1-31')<=5 or DATEDIFF(day,Birthday,'2004-1-31')>=-5

解决方案 »

  1.   

    上面的不對,看錯了
    select * from 员工表 where DATEDIFF(day,Birthday,'2004-1-31')<=5
      

  2.   

    上面的寫的還是不對select * from 员工表 where DATEDIFF(day,Birthday,'2004-1-31')<=5 and DATEDIFF(day,Birthday,'2004-1-31')>0
      

  3.   

    where  dateadd(year,1-datepart(year,Birthday))- '1901-1-31' between 0 and 5
      

  4.   

    错了是:
    where  dateadd(year,1901-datepart(year,Birthday),Birthday)- '1901-1-31' between 0 and 5
    才对
      

  5.   

    还是不对。
    完整的逻辑是:一句话逻辑不好理解,所以写成几句
    declare @t int --几天
    declare @x datetime --哪天
    set @t=5
    set @x=(select getdate())
    set @x=dateadd(year,1901-datepart(year,@x),@x)
    where  dateadd(year,1901-datepart(year,Birthday),Birthday)- 
    (case when datepart(year,Birthday)<>datepart(year,Birthday-@t) then dateadd(year,1,@x) else @x end )between 0 and @t
      

  6.   

    select * from 员工表 where DATEDIFF(day,Birthday,'2004-1-31')<=5 and DATEDIFF(day,Birthday,'2004-1-31')>0运行一下试试!
      

  7.   

    还是想错了。。
    where  dateadd(year,1901-datepart(year,Birthday),Birthday)- 
    (case when datepart(year,Birthday)<>datepart(year,Birthday-@t) then dateadd(year,-1,@x) else @x end )between 0 and @t
      

  8.   

    select * from 员工表 where (datepart(month,birthday) = datepart(month,getdate()))
    and ((datepart(date,birthday) - datepart(date,getdate())) <= 5)
      

  9.   

    上面错了。
    select * from 员工表 where datediff(date,getdate(),birthday) <= 5
      

  10.   

    上面又错了.
    select * from 员工表 where datediff(date,getdate(),birthday)as rec_birghday between 0 and 6
      

  11.   

    select * from bos_sales_head where datediff(day,getdate(),birthday) between 0 and 5
      

  12.   

    create table t1 (dt smalldatetime)
    insert t1 values('2000-01-01')
    insert t1 values('2000-11-01')
    insert t1 values('2001-01-01')
    insert t1 values('2004-01-30')
    insert t1 values('2004-01-31')
    insert t1 values('2004-02-01')
    insert t1 values('2004-02-05')
    insert t1 values('2004-02-06')select * , datediff(day,getdate(),dt) from t1
    where datediff(day,getdate(),dt) between 1 and 5
      

  13.   

    --既然有人贴测试数据:create table #tmp (Birthday smalldatetime)
    insert #tmp values('2000-01-31')
    insert #tmp values('2000-11-29')
    insert #tmp values('2001-01-01')
    insert #tmp values('2004-01-30')
    insert #tmp values('2004-01-31')
    insert #tmp values('2004-02-01')
    insert #tmp values('2004-02-05')
    insert #tmp values('2004-02-06')
    declare @t int --几天
    declare @x datetime --哪天
    set @t=5
    set @x=(select getdate())
    set @x=dateadd(year,1901-datepart(year,@x),@x)select *   from #tmp
    where  dateadd(year,1901-datepart(year,Birthday),Birthday)- 
    (case when datepart(year,Birthday)<>datepart(year,Birthday-@t) then dateadd(year,-1,@x) else @x end )between 0 and @t/*
    Birthday                                               
    ------------------------------------------------------ 
    2000-01-31 00:00:00
    2004-01-31 00:00:00
    2004-02-01 00:00:00(所影响的行数为 3 行)
    */