我用三个edit来分别输入年、月、日,然后再组合成一个日期形式的值,再用另外三个edit来分别输入年、月、日,也是组合成一个日期形式的值,用这两个值去查某个日期范围(如出生日期的范围),不知道应该怎么做呢?注:后台数据库是SQL Server2000 ,而那字段‘出生日期’的类型只能是datetime类型。还有,最好是能判别出这两个值(即两个日期)的哪一个在前,哪一个在后,这样就不会那么容易出错了。

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Date1, Date2: TDateTime;
    begin
      Date1 := EncodeDate(StrToIntDef(Edit1.Text), StrToIntDef(Edit2.Text), StrToIntDef(Edit3.Text));
      Date2 := EncodeDate(StrToIntDef(Edit4.Text), StrToIntDef(Edit5.Text), StrToIntDef(Edit6.Text));
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('Select * from TableName Where aDate between :Date1 and :Date2');
        Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
        Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
        Open;
      end;
    end;
      

  2.   

    呵呵, 着急抢分, 上面的代码拉下了默认值
    Uses Math;假设Edit1/Edit2/Edit3为第一年月日, Edit4/Edit5/Edit6为第二个年月日procedure TForm1.Button1Click(Sender: TObject);
    var
      Date1, Date2: TDateTime;
    begin
      Date1 := EncodeDate(StrToIntDef(Edit1.Text, 2004), StrToIntDef(Edit2.Text, 1), StrToIntDef(Edit3.Text, 1));
      Date2 := EncodeDate(StrToIntDef(Edit4.Text, 2004), StrToIntDef(Edit5.Text, 1), StrToIntDef(Edit6.Text, 1));
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('Select * from TableName Where aDate between :Date1 and :Date2');
        Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
        Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
        Open;
      end;
    end;
      

  3.   

    还是不行。运行后输入日期查询时,就出现下面这个错误提示:
    Access violation at address 004A7D1D in module 'BOOK.EXE'. Read of address FFFFFFFF'
    另外,编译时还会出现一个警告:[Warning] book.pas(300): Variable 'date1' might not have been initialized
    请问上面这错误和警告怎样解决啊??
      

  4.   

    var
       Select_SQL,BigDatetime,smallDatetime:string;
    begin
      // 获取时间信息
      if DateTimePickerStart.DateTime>DateTimePickerEnd.DateTime then
         begin
          Application.MessageBox('起始日期不能大于结束日期,请重新输入!', '提示', MB_OK + MB_ICONWARNING);
          DateTimePickerStart.SetFocus;
          abort;
         end
      else
        begin
          BigDatetime:=FormatDateTime('yyyymmdd',DateTimePickerStart.DateTime);
          smallDatetime:=FormatDateTime('yyyymmdd',DateTimePickerEnd.DateTime);
        end;
      // 设置查询条件
      Select_SQL:='select Gtin as  全球贸易项目代码 , Cn_Name as 产品中文名称 , Editor as 用户名 , InsertTime as 录入时间  From  Baseinfo '
                  +' where InsertTime>='''+ Own_Date_Format(BigDateTime)+' 00:00:00'+'''  and  InsertTime<='''+Own_Date_Format(SmallDateTime)+' 23:59:59'+'''';
      if Trim(Man_edit.Text) <>'' then
         Select_SQL:=Select_SQL+ ' and  Editor= '''+ trim(Man_edit.Text)+ '''';
      if Trim(DM_edit.Text)<>'' then
         Select_SQL:=Select_SQL+'  and  gtin= '''+Trim(DM_edit.Text)+'''';
         Select_SQL:=Select_SQL+'  Order by Gtin ';
    Function Own_Date_Format(datastr:string): string;
    begin
      if length(datastr)<> 10 then
          result:='';
          result:=copy(datastr,1,4)+'-'+copy(datastr,5,2)+'-'+copy(datastr,7,2);
    end;
      

  5.   

    测试通过
    环境:   SQL Server 2000 + Win2003 + Delphi 6
    数据库: NorthWind
    测试数据:Edit1..6分别为 1996 01 01 1999 01 01;procedure TForm1.Button1Click(Sender: TObject);
    var
      Date1, Date2: TDateTime;
    begin
      Date1 := EncodeDate(StrToIntDef(Edit1.Text, 2004), StrToIntDef(Edit2.Text, 1), StrToIntDef(Edit3.Text, 1));
      Date2 := EncodeDate(StrToIntDef(Edit4.Text, 2004), StrToIntDef(Edit5.Text, 1), StrToIntDef(Edit6.Text, 1));
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('Select * from Orders Where OrderDate between :Date1 and :Date2');
        Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
        Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
        Open;
      end;
    end;
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Date1, Date2: TDateTime;
    begin
      Date1 := EncodeDate(StrToInt(Edit1.Text), StrToInt(Edit2.Text), StrToInt(Edit3.Text));
      Date2 := EncodeDate(StrToInt(Edit4.Text), StrToInt(Edit5.Text), StrToInt(Edit6.Text));
      with ADOquery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('Select * from tablename  Where adate between :Date1 and :Date2');
        Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
        Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
        Open;
      end;
    end;end.
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Date1, Date2: TDateTime;
    begin
      Date1 := EncodeDate(StrToIntDef(Edit1.Text), StrToIntDef(Edit2.Text), StrToIntDef(Edit3.Text));
      Date2 := EncodeDate(StrToIntDef(Edit4.Text), StrToIntDef(Edit5.Text), StrToIntDef(Edit6.Text));
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('Select * from TableName Where aDate between :Date1 and :Date2');
        Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
        Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
        Open;
      end;
    end;
      

  8.   

    各位大虾,编译是没错的。不过就出现了更大的问题了,就连我以前可以运行的都出现这个问题了,我刚才也说过了的,就是:Access violation at address 004A7D1D in module 'BOOK.EXE'. Read of address FFFFFFFF'。这个错误在我每次运行时都会出现的(包括我之前运行没错的)。这个错误是指adoQuery有错误,好象是系统的问题,是不是啊?请问大虾,这究竟是什么原因啊,有什么办法解决啊???
      

  9.   

    var std1,std2:string;
    begin
      with DataM.InpBillADOQ do
      begin
        close;
        sql.Clear;
        sql.Add('select a.item_name,a.item_spec,a.amount,a.units,a.costs/a.amount,a.costs, ');
        sql.Add('b.drug_insur_type1||b.pay_pay1,a.billing_date_time ');
        sql.Add('from inp_bill_detail a,price_vs_insur b ');
        sql.Add('where a.item_code=b.item_code ');
        sql.Add('and a.patient_id='''+trim(IdEdit.Text)+''' ');
        sql.Add('and trunc(a.billing_date_time) >=:std1 ');
        sql.add('and trunc(a.billing_date_time) <=:std2 ');
        sql.Add('order by a.billing_date_time ');
        Parameters.ParamValues['std1']:=strToDateTime(DateToStr(BeginDateTimeP.Date)+' 00:00:00');
        Parameters.ParamValues['std2']:=StrToDateTime(DateToStr(EndDateTimeP.Date)+' 23:59:59');
        prepared;
        open;
        //////////////////////////////////////////////////////////////
        DataM.SumADOQ.Close;
        DataM.SumADOQ.SQL.Clear;
        std1 := datetostr(BeginDateTimeP.Date)+' 00:00:00'   ;
        std2 := datetostr(EndDateTimeP.Date)+' 23:59:59';
        DataM.SumADOQ.SQL.Add('select sum(costs)  from inp_bill_detail ');
        DataM.SumADOQ.sql.Add('where patient_id='+''''+trim(IdEdit.Text)+'''');
        DataM.SumADOQ.sql.Add(' and billing_date_time>='+ 'to_date('+''''+std1+''''+','+''''+'yyyy-mm-dd hh24:mi:ss'+''''+')');
        DataM.SumADOQ.sql.add(' and billing_date_time<='+ 'to_date('+''''+std2+''''+','+''''+'yyyy-mm-dd hh24:mi:ss'+''''+')');
        DataM.SumADOQ.open;
        Label10.Caption:=DataM.SumADOQ.Fields[0].AsString;
        DataM.SumADOQ.Close;
        //////////////////////////////////////////////////////////////
        if recordcount<1 then
          application.MessageBox('该病人在此时间段内没有发生费用!','系统提示',32);
          exit;
          close;
      end;
    end;