有两个Tdatetimepicker控件,一个控件表示开始,一个表示结束,我要查询在这两个时间之间的数据,用sql怎么表示?我用sql.add('select*from table where 时间>='+datetostr(datetimepicker1.date)+'and 时间<='+datetostr(datetimepicker2.date));没有用,也没有错误,请问那有错?

解决方案 »

  1.   

    看看你表中的数据字段是字符串型吗,若不是就不需要类型转换.
    另外,日期不能放在引号外面.
    Sql.add(' select * from table where 时间 >= datetimepicker1.date and 时间 <=
            datetimepicker2.date');
    这样就可以了.
      

  2.   

    sql.add('select*from table where 时间>='+datetimepicker1.date+'and 时间<='+datetimepicker2.date);
    如果你在数据库中用字符串来存储时间就不好这么写了。
      

  3.   

    是oracle数据库吗?是的话可以这样写
    sql.add('select * from table where to_char(时间字段)>='+datetostr(datetimepicker1.date)+' and  to_char(时间字段)<='+datetostr(datetimepicker2.date));
    也可以这样写
    sql.add('select * from table where 时间字段>=to_date('''+datetostr(datetimepicker1.date)+''',''yyyy-mm-dd'') and 时间字段<=to_date('''+datetostr(datetimepicker2.date)+''',''yyyy-mm-dd''));
      

  4.   

    可以用动态参数吗,因为delphi不支持在sql语句中写日期
      

  5.   

    可以用动态参数,因为delphi不支持在sql语句中写日期
    可以呀
    with query1 do 
    begin
      close;
      sql.clear;
      sql.add(sql语句);
      parambyname(字段).value:=value值
      open;
    end;