我现在有个时间变量date1,,date2 想与表中Time字段中值做比较,选出大于或小于的记录 date1的形式为YYYY-mm-dd 而表中设置字段Time的类型为vachar(20),要怎么写语句,请大侠帮忙修改下列语句
with query1 do
begin
  close;
  SQL.Clear;
  SQL.Add('select × from PlanInfo,ProInfo where PlanInfo.ID =ProInfo.ID ');
  SQl.Add('and  Time > date1 and TimePlanEnd < date2');
  Open;
end;

解决方案 »

  1.   

    如果date1, date2字段是datetime类型,
    with query1 do 
    begin 
      close; 
      SQL.Clear; 
      SQL.Add('select × from PlanInfo,ProInfo where PlanInfo.ID =ProInfo.ID '); 
      SQl.Add('and  Time > convert(varchar(20), date1, 120) and TimePlanEnd < convert(varchar(20), date2, 120)'); 
      Open; 
    end;
      

  2.   


    with query1 do 
    begin 
      close; 
      SQL.Clear; 
      SQL.Text:='select * from PlanInfo,ProInfo where PlanInfo.ID =ProInfo.ID AND convert(varchar(10),Time,120) >'+QuotedStr(date1)+' AND convert(varchar(10),TimePlanEnd,120) <'+QuotedStr(date2);
      Open; 
    end;
      

  3.   

    补充一下:T-SQL函数:
    CAST 和 CONVERT
    将某种数据类型的表达式显式转换为另一种数据类型。CAST 和 CONVERT 提供相似的功能。语法
    使用 CAST:CAST ( expression AS data_type ) 使用 CONVERT:CONVERT (data_type[(length)], expression [, style])expression是任何有效的 Microsoft® SQL Server™ 表达式。有关更多信息,请参见表达式。 data_type目标系统所提供的数据类型,包括 bigint 和 sql_variant。不能使用用户定义的数据类型。有关可用的数据类型的更多信息,请参见数据类型。 lengthnchar、nvarchar、char、varchar、binary 或 varbinary 数据类型的可选参数。 style日期格式样式,借以将 datetime 或 smalldatetime 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型);或者字符串格式样式,借以将 float、real、money 或 smallmoney 数据转换为字符数据(nchar、nvarchar、char、varchar、nchar 或 nvarchar 数据类型)。SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。
      

  4.   

    上面两种办法都试了,说[Error] Unit_FormCommonQuery.pas(290): Incompatible types: 'String' and 'TDate'
    date1和date2是Tdate 形式yyyy-mm-dd,数据库表中Time的类型为vachar(20),形式也为yyyy-mm-dd,大侠再帮帮忙
      

  5.   


    with query1 do 
    begin 
      close; 
      SQL.Clear; 
      SQL.Text:='select * from PlanInfo,ProInfo where PlanInfo.ID =ProInfo.ID AND '+
                'convert(varchar(10),Time,120) >:date1 AND convert(varchar(10),TimePlanEnd,120) <:date2';
      parameters.Parambyname('date1').value := date1;
      parameters.Parambyname('date2').value := date2;
      Open; 
    end;