SQL语句查询,两个参数Time1、Time2,表示两个时间,要查询Time1 ~Time2的数据,表名Sending表,要求,查询的Time1的格式是"2012-9-1 00: 00: 00",Time2的格式是"2012-9-4 23:59:59",该如何写SQL语句呢?请贴出代码

解决方案 »

  1.   

    declare @time1 date = '2012-9-1'
    declare @time2 date = '2012-9-5'select *
    from  sending
    where 时间列 >= @time1
    and 时间列 < @time2
      

  2.   

    select left(convert(varchar(25),time1,120),10)+' 00:00:00' as time1,left(convert(varchar(25),time2,120),10)+' 23:59:59' as time2 
    from sending
    -->有个测试结果,time1和time2用的是getdate()
    /*
    time1                         time2                         
    ----------------------------- ----------------------------- 
    2012-09-07 00:00:00           2012-09-07 23:59:59
    */
      

  3.   

    如果time1,time2字段本身不是datetime型
    如下:
    select time1=convert(varchar(8),time1,120)+' 00:00:00' ,time2=convert(varchar(8),time2,120)+' 23:59:59' 
    from Sending
      

  4.   

    想要的代码是:Select * from Sending where submittime >=Time1 and Time2<=submittime 其中的Time1和Time2该怎么写才能查询到 2012-9-1 00:00:00 到2012-9-5 23:59:59的数据? 
      

  5.   

    select * from Sending 
    where submittime  between convert(varchar(8),time1,120)+' 00:00:00' and convert(varchar(8),time2,120)+' 23:59:59' --orSelect * from Sending where submittime >=convert(varchar(8),time1,120)+' 00:00:00' and Time2<=convert(varchar(8),time2,120)+' 23:59:59'
    from Sending
      

  6.   

    @time1 ='2012-09-01 00:00:00'
    @time2 ='2012-09-05 23:59:59'
    Select * from Sending where submittime between  @Time1 and @Time2
      

  7.   


    declare @time1 datetime
    declare @time2 datetimeset @time1='2012-9-1 00:00:00'
    set @time2='2012-9-5 23:59:59'select * from sending
    where submittime>=@time1 and submittime<=@time2
      

  8.   

    如果你的submittime 是datetime类型 可以直接如下查询declare @Time1 datetime,@Time2 datetime
    set @Time1='2012-9-1'
    set @Time2='2012-9-5'
    Select * from Sending where submittime >dateadd(dd,-1,@Time1) and submittime< dateadd(dd,1,@Time12)
      

  9.   


    declare @Time1 datetime,@Time2 datetime
    set @Time1='2012-9-1 00:00:00'
    set @Time2='2012-9-5 23:59:59'select * from sending
    where submittime>=@time1 and submittime<=@time2
    --or
    select * from sending
    where submittime between @Time1 and @Time2
      

  10.   

    大家都误解了我的意思,我传递的是一个参数,不是固定日期的,参数是Time1和Time2,要根据传递的参数进行查询的
      

  11.   

    把1#楼的改成一个存储,@Time1和@Time2作为参数,不就是你想要的么?
      

  12.   

    select * from sending where time in time1 and time2
      

  13.   

    select * from sending where timecolumnname in time1 and time2
      

  14.   

    就用你这个句子就可以了
    只是你写错了 
    改成  Select * from Sending where submittime >=Time1 and Time2>=submittime--后面一个大于等于号的方向反了
      

  15.   

    大家都没误会你的意思,楼上全部说的都是将Time1和Time2认为是参数
      

  16.   

    我把实际情况说下把,用的是ACCESS数据库,用的是asp.net,然后呢,数据库里面的日期是用文本格式,录入进去的格式是2012-9-1 8:40:20,2012-9-4 17:30:20 ,根据参数传递来查询这个日期段即2012-9-1 00:00:00 到2012-9-4 23:59:59 之间的数据,但是查询的结果是只能查出的数据却少了,看了下测试查询语句的,发现时2012-9-1 8:40:20 和2012-9-4 17:30:20比较时,是8>17来比较的,这让我十分的无语,我不知道怎么写才正确
      

  17.   

    那你将这两个传进来的日期时间字符串用convert(datetime,dt)后再比较
      

  18.   

    实在没有办法解决,用的是ACCESS数据库,设置的是文本格式,导入的日期也是文本格式,但是导出的日期参数却是2012-9-1 00:00:00的日期格式,完全没有办法比较的大小