如何用datediff求出从今天开始往后最近一个月的数据,这样的搜索条件放在where语句中,应该怎么写?

解决方案 »

  1.   

    select * from tb where 时间字段 between getdate() and DATEADD(mm,1,getdate())
      

  2.   

    select convert(varchar(10),DATEADD(d,number,getdate()),120)dt 
    from master..spt_values 
    where type='p' and number<31 and DATEADD(d,number,getdate())<CONVERT(varchar(10),dateadd(m,1,getdate()),101)
    order by 1
    /*
    dt
    ----------
    2011-09-22
    2011-09-23
    2011-09-24
    2011-09-25
    2011-09-26
    2011-09-27
    2011-09-28
    2011-09-29
    2011-09-30
    2011-10-01
    2011-10-02
    2011-10-03
    2011-10-04
    2011-10-05
    2011-10-06
    2011-10-07
    2011-10-08
    2011-10-09
    2011-10-10
    2011-10-11
    2011-10-12
    2011-10-13
    2011-10-14
    2011-10-15
    2011-10-16
    2011-10-17
    2011-10-18
    2011-10-19
    2011-10-20
    2011-10-21(30 行受影响)
    */
      

  3.   

    放在where条件:
    where 日期字段>=convert(varchar(10),getdate()120) and 日期字段<=CONVERT(varchar(10),dateadd(m,1,getdate()),101)
      

  4.   

    日期字段>=convert(varchar(10),getdate(),120) and 日期字段<=CONVERT(varchar(10),dateadd(m,1,getdate()),101)