DATEPART(year, StartTime)=2002 and DATEPART(month, StartTime)=5或CONVERT (char(6), StartTime, 20)='2002-05'

解决方案 »

  1.   

    where year(StartTime)=2002 and month(StartTime)=5
      

  2.   

    to 空空大师:
    应为char(7)
    CONVERT (char(7), StartTime, 20)='2002-05'
    StartTime>='2002-05-1' and StartTime<'2002-06-01'
      

  3.   

    比如一个数据表中其中有个字段是日期型StartTime
    有一条记录的值为2002-6-28 12:00:00
    用SQL语句查询出2002年5月的所有记录,
    select * from User where to_char(startTime,'YYYY-MM-DD HH24:MI:SS')>'2002-6-22 00:00:00'
      

  4.   

    last week:
    where datepart(ww,StartTime)=datepart(ww,getdate())-1
      

  5.   

    select * from User where to_char(startTime,'YYYY-MM-DD HH24:MI:SS')>'2002-6-22 00:00:00'
    上周的, sorry
      

  6.   

    last 1 - 2 weekwhere datepart(ww,StartTime)>=datepart(ww,getdate())-2
    and datepart(ww,StartTime)<=datepart(ww,getdate())-1
      

  7.   

    StartTime>='2002-05-1' and StartTime<'2002-06-01'