大家好,请问下面的问题如何解决?insert into Status values('Channel1', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:45:55', '12.35')
insert into Status values('Channel2', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:45:55', '12.35')
insert into Status values('Channel3', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:45:55', '12.35')
insert into Status values('Channel4', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:51:55', '12.35')
insert into Status values('ChannelSum', '1.32', '0.80', '4.92', 'OK', '2010-07-23 8:45:55', '12.35')
insert into Status values('Channel1', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:51:55', '12.35')
insert into Status values('Channel2', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:51:55', '12.35')
insert into Status values('Channel3', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:51:55', '12.35')
insert into Status values('Channel4', '0.33', '0.20', '1.23', 'OK', '2010-07-23 8:51:55', '12.35')
insert into Status values('ChannelSum', '1.32', '0.80', '4.92', 'OK', '2010-07-23 8:51:55', '12.35')
我在数据库中插入如上的时间
然后我用
strFirstTime += " 00:00:00" ;
strSecondTime += " 23:59:59";
vSQL = _T("SELECT * FROM Status WHERE Time between '") + strFirstTime + _T("'")
+ _T(" and '") + strSecondTime + _T(" ' ") + _T("ORDER BY Time DESC");strFirstTime = strSecondTime = 2010-07-23
为什么查不到任何数据呢?
请高手指点

解决方案 »

  1.   

    最好把time 的也换成" 00:00:00" 这种格式
      

  2.   

    strFirstTime = strSecondTime = 2010-07-23strFirstTime += " 00:00:00" ;
    strSecondTime += " 23:59:59";
    出在这吧?
    你这俩值打印出来是多少??
      

  3.   

    感谢wxf163的回复
    请问如果向我这样的问题,想查询一段时间,如何编写程序阿
    能够给我一个示例。
      

  4.   

    为什么我改写为
    select * from Status where Time = '2010-07-23 8:51:55'
    就可以找到相应的数据阿如何查找一段时间内的数据阿?请高手帮忙
      

  5.   

    declare @begin nvarchar(20)
    declare @end nvarchar(20)set @begin ='2010-07-23'
    set @end ='2010-07-23'
    set @begin =@begin+ ' 00:00:00'
    set @end =@end + ' 23:59:59'
    declare @sql nvarchar(2000)
    set @sql = 'SELECT * FROM Status WHERE Time between '''+@begin +''' and '''+ @end + ''' ORDER BY Time DESC'
    print @sql
      

  6.   

    请问wxf163,您这样打印出来的sql与我写的sql程序一致,但是看不到要查找的数据阿
    如何才能查找一段时间内的数据呢?
      

  7.   

    写入数据库的时间格式不对,必须 hh:mm:ss 就是8:45:55 要改为 08:45:55
      

  8.   

    加个时间格式转换试试
    use ymw_test
    if object_id('Status') is not null
    drop table [Status]
    create table [Status]([Time] datetime)
    goinsert into [Status]
    select getdate()
    go
    insert into [Status]
    select getdate()
    go
    insert into [Status]
    select getdate()
    go
    insert into [Status]
    select '2010-07-23 9:37:45.280'
    declare @begin nvarchar(20)
    declare @end nvarchar(20)set @begin ='2010-07-23'
    set @end ='2010-07-23'
    set @begin =@begin+ ' 00:00:00'
    set @end =@end + ' 23:59:59'
    declare @sql nvarchar(2000)
    set @sql = 'SELECT * FROM Status WHERE convert(datetime,[Time],120) between '''+@begin +''' and '''+ @end + ''' ORDER BY Time DESC'
    print @sql
    exec(@sql)
    /*
    Time
    -----------------------
    2010-07-23 09:45:11.390
    2010-07-23 09:45:11.390
    2010-07-23 09:45:11.357
    2010-07-23 09:37:45.280(4 行受影响)
    */