在数据库里面时间的存储形如: 2011-9-18 8:16:26
是字符串类型想问一下如果想查询 2011年9月1号到2011年10月1号的数据,该如何处理?Thanks!!!!

解决方案 »

  1.   


    select * from tb where convert(varhcar(10),日期时间字段,120) between '2011-09-01' and '2011-10-01'
      

  2.   

    select * from tb where convert(datetime,dt)>='2011-09-01' and convert(datetime,dt)<'2011-10-02'
      

  3.   

    select * from tb where col >'2011-09-01' and col<'2011-10-01'--不经转换,来得更快些...
      

  4.   

    Thanks!
    另外的问题,因为其他地方用了“日历”选择,日期的形式是2011-9-1,不是2011-09-01
    所以查询有问题。
      

  5.   

    select * from tb where convert(varchar(10),时间字段,120) between '2011-09-01' and '2011-10-01'
      

  6.   

    how?多谢!
      

  7.   

    那就用convert转换select convert(varchar(10),cast('2011-9-1' as datetime),120)
    /*
    ----------
    2011-09-01(1 行受影响)
    */
      

  8.   

    declare @s datetime
    set @s='2011-9-1'
    select convert(varchar(10),CAST(@s as datetime),120)/*----------
    2011-09-01(1 行受影响)*/
      

  9.   

    还是不行,现在的code是
    select * from tb where convert(varchar(10),systemtime,120) between convert(varchar(10),cast('2011-9-1' as datetime),120) and convert(varchar(10),cast('2011-10-1' as datetime),120)能查到18号的,但不能查到7号的数据但如果
    select * from tb where convert(varchar(10),systemtime,120) between '2011-9-1' and '2011-9-9'
    就能查到7号的数据
      

  10.   

    select * from tb where convert(varchar(10),systemtime,120) between '2011-9-1' and '2011-10-1'能查到18号的,但不能查到7号的