我用delphi自带的桌面数据工具(database desktop)建立了一个表,表中有一个日期时间型字段
tj_query.SQL.Clear ;
tj_query.Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>='''+datetostr(begin_date.date)+''''+' and rq<='''+datetostr(end_date.date)+'''';
tj_query.SQL.add(sqlstr);
tj_query.Open;
为什么老是报错?类型不匹配
错误在日期时间的比较上
我该怎么办?
我不想用过滤

解决方案 »

  1.   

    tj_query.SQL.Clear ;
    tj_query.Close;
    sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
    sqlstr:=sqlstr+' and rq>='''+datetostr(begin_date.date)+''''+' and rq<='''+datetostr(end_date.date)+'''';
    tj_query.SQL.add(sqlstr);
    tj_query.Open;改一改看:tj_query.SQL.Clear ;
    tj_query.Close;
    sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
    sqlstr:=sqlstr+' and rq>=:P_BeginDate and rq<=:P_EndDate';
    tj_query.SQL.add(sqlstr);
    tj_Query.Parameters.ParamByName('P_BeginDate').value := datetostr(begin_date.date);
    tj_Query.Parameters.ParamByName('P_EndDate').value := datetostr(end_date.date)
    tj_query.Open;
      

  2.   

    在SQL语句中时间类型应该用“#”括起来而不是用单引号。
    正确写法为
    tj_query.SQL.Clear ;
    tj_query.Close;
    sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
    sqlstr:=sqlstr+' and rq>=#'+datetostr(begin_date.date)+'#'+' and rq<=#'+datetostr(end_date.date)+'#';
    tj_query.SQL.add(sqlstr);
    tj_query.Open;
      

  3.   

    你后台用的是什么数据库?不同的数据库,把时间日期括起来的符号是不一样的,Access的是“#”,Oracle有它自己定义的格式。
      

  4.   

    with tj_query do
      begin
        SQL.Clear ;
        Close;
        sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
    sqlstr:=sqlstr+' and rq>=:a and rq<=:b';
        SQL.add(sqlstr);
        Parameters.ParamByName('a').asdatetime :=FormatDatetime('yyyy-mm-dd',begin_date.date);
        Parameters.ParamByName('b').asdatetime :=FormatDatetime('yyyy-mm-dd',end_date.date);
       Open;
     end;
      

  5.   

    我一般转化成string然后比较。
      

  6.   

    to : huifei99(天之痕) 
    说的是Access库中的用法。
    建议用参数 
    总结方法:
    跟踪一下 
    吧SQl语句付给一个字符串 然后到查询分析器去验证一下,看看什么错误!