我想删除每天某一时间段的数据,如想删除17:30---08:00的加班数据,请问SQL如何写?偶是菜鸟,望赐教
数据库结构如下: ID             FTIME              DEVICENO
               12232     2007-3-23  09:23:00        1
               23334     2007-3-23  09:23:00        2

解决方案 »

  1.   

    delete from tabel1 where (datepart(hh,ftime)>17 and datepart(n,ftime)>30) and datepart(hh,ftime)<8
      

  2.   

    delete from table where FTIME between '2008-05-03 17:30' and '2008-05-03 08:00'delete from table where FTIME>='2008-05-03 17:30' and FTIME<='2008-05-03 08:00'
      

  3.   

    delete from tabel1 where (datepart(hh,ftime)>17 and datepart(n,ftime)>30) or datepart(hh,ftime)<8
      

  4.   


    --> 测试数据: @s
    declare @s table (ID int,FTIME datetime,DEVICENO int)
    insert into @s select 12232,'2007-3-23 09:23:00',1 
    union all select 23334,'2007-3-23 09:23:00',2
    union all select 23334,'2007-3-23 17:23:00',2
    union all select 23334,'2007-3-23 17:32:00',2
    union all select 23334,'2007-3-23 07:23:00',2delete @s where 
    ftime between convert(varchar(10),ftime,120)+' 17:30:00' and convert(varchar(10),dateadd(dd,1,ftime),120)+' 08:00:00'
    or
    ftime between convert(varchar(10),dateadd(dd,-1,ftime),120)+' 17:30:00' and convert(varchar(10),ftime,120)+' 08:00:00'select * from @s
      

  5.   

    顶ganggang2008 简洁明了。 
      

  6.   


    一样的:
    delete from tab where ftime>='2008-5-1 0:0:0' and ftime<'2008-6-1 0:0:0'
      

  7.   

    delete tb where convert(char(8),ftime,108) between '17:30:00' and '08:00'
      

  8.   

    delete tb 
    where convert(char(8),ftime,108) > '17:30:00'
       or convert(char(8),ftime,108) < '08:00:00'
      

  9.   

    delete tb where convert(char(8),ftime,108) not between '17:30:00' and '08:00'
      

  10.   

    不好意思,看错了。应该是delete from tab where ftime>='2008-5-1 0:0:0' and ftime<'2008-6-1 0:0:0' and (datepart(hh,ftime) * datepart(n,ftime) >= 17*60+30 and datepart(hh,ftime)<8
      

  11.   


    delete from tab where ftime>='2008-5-1 0:0:0' and ftime<'2008-6-1 0:0:0' and 
    datepart(hh,ftime) * datepart(n,ftime) >= 17*60+30 and datepart(hh,ftime)<20