现有一个表table_12
表结构:
——————————————————————————————————————————
字段名    类型
id                   int(自增)
name              nvarchar(50)
b_time            datetime
e_time            datetime
————————————————————————————————————————————记录:
——————————————————————————————————————————————————
id            name             b_time                   e_time
1             n_n1             9:30:00                12:30:00 
2             n_n2             9:30:00                20:40:00  
...              ...                  ...                          ...
sql检索条件要满足以本地机时间为准,并在b_time和e_time范围内就让记录显出来,也就b_time<=now()<=e_time虽数定数据库的程序也写得很多了,但唯独这种情况是第一次遇到。特在此请教了。sqlp="select ID, p_name, P_Price, P_kwei, B_time, E_time from kc_products where c_id="&rskc("id")&" and getdate() between convert(char(11),getdate(),120)+b_time and convert(char(11),getdate(),120)+e_time"这样竟然一条记录也读不出来,程序也不报错.打印出来的SQL语句是这样的:select ID, p_name, P_Price, P_kwei, B_time, E_time from kc_products where c_id=16 and getdate() between convert(char(11),getdate(),120)+b_time and convert(char(11),getdate(),120)+e_time getdate() 当前时间是:10:00:00,大于10点的记录有很多,就是读不出来

解决方案 »

  1.   

    select convert(varchar,getdate(),108)
    --10:27:40
    用这个
      

  2.   

    select len(convert(varchar(11),getdate(),120))
    --10
    也就是说最后的空格被自动处理掉了,楼主如果还像用原来的方法那就手动添加空格+''+
      

  3.   


    select convert(varchar(11),getdate(),120)+'10:23:23'
    --2006-08-30 10:23:23
    不好意思,我的len()居然有rtrim()的功效.在不用len()的时候空格确实没有消失,..恩再想个变通的办法...
      

  4.   

    create table test(id int identity,name nvarchar(50),b_time datetime,e_time datetime)
    insert into test select 'a','10:13:23','10:53:23'
    insert into test select 'a','7:23:23','11:23:23'
    insert into test select 'a','5:23:23','8:23:23'
    -----得到如下结果集
    1 a 1900-01-01 10:13:23.000 1900-01-01 10:53:23.000
    2 a 1900-01-01 07:23:23.000 1900-01-01 11:23:23.000
    3 a 1900-01-01 05:23:23.000 1900-01-01 08:23:23.000
    --
    即数据库默认会填满datetime类型的年月日.所以你的处理要改一下,表示你想象中那种拼接.
      

  5.   

    convert(char(11),getdate(),120)+convert(char(8),b_time,108)
    --如下就可以了
    select ID, name, B_time, E_time 
    from test 
    where getdate() between convert(char(11),getdate(),120)+convert(char(8),b_time,108) 
    and convert(char(11),getdate(),120)+convert(char(8),e_time,108)