Search code examples
sql-serverdelphifiredac

EConvertError when I Filter my sqlServer database with FireDac


I'm using Delphi 10.2.3 Tokyo with FireDac , and setting a connection to an SqlServer(2016) database , when i set the filter Date_Document field in my query I get "could not parse SQL timesamp string" Exception.

BTW:Data type of Date_Document is datetime.

procedure TfrmMain.fltDateEdt2Change(Sender: TObject);
var dat1,dat2:TSQLTimeStamp;
begin
if (fltDateEdt1.Text<>'  /  /    ') and (fltDateEdt2.Text<>'  /  /    ') then
 begin
 if fltDateEdt1.Date<fltDateEdt2.Date then
  try
   dat1:=DateTimeToSQLTimeStamp(fltDateEdt1.Date);
   dat2:=DateTimeToSQLTimeStamp(fltDateEdt2.Date);
   comps.qryMain.Filter:=format('%s>= %s AND %s<=%s',  ['DATE_DOCUMENT',SQLTimeStampToStr('YYYY-MM-DD',dat1).QuotedString,'DATE_DOCUMENT',SQLTimeStampToStr('YYYY-MM-DD',dat2).QuotedString]);
comps.qryMain.Filtered:=true;
  except

  end;
  end
else
  begin
  comps.qryMain.Filtered:=false;
  comps.qryMain.Filter:='';
  end;
end;

Solution

  • I would use escape sequences in such case. Like for example (assuming that DATE_DOCUMENT column is of DATETIME data type as you mentioned):

    comps.qryMain.Filter := Format('DATE_DOCUMENT >= {dt %s 00:00:00} AND DATE_DOCUMENT <= {dt %s 23:59:59}', [
      FormatDateTime('yyyy-mm-dd', fltDateEdt1.Date),
      FormatDateTime('yyyy-mm-dd', fltDateEdt2.Date)
    ]);
    

    The point of making such filter string is that you let FireDAC convert described date time format into DBMS' native one by only replacing date portion with the given date time in such specified format.

    Of even better, I would simply write a handler for the OnFilterRecord event.