如何按DATE值常量过滤FireDAC数据集?

时间:2016-12-29 15:23:58

标签: delphi firebird firedac

procedure TForm2.Button1Click(Sender: TObject);
begin
  with qryWithFilter do
  begin
    Filtered := False;
    OnFilterRecord := nil;
    // date separator may be used any from [' ', '-', '\', '.', '/']
    // Filter := 'DATA_NAS < (''DatetoStr(date3)'')';
    // Filter := 'DATA_NAS < ''28/06/1939''';
    // Filter := 'DATA_NAS < (FormatDateTime(''dd/mm/yyyy'', ''28/06/1968''))';
    // Filter := 'DATA_NAS < TO_DATE(''1996-07-29'',''yyyy-mm-dd'')';
    Filter := 'DATA_NAS < (TO_DATE(''1996-07-29'',''yyyy-mm-dd''))';
    Filtered := True;
  end;
end;

仅适用于&#39; 28/06/1968&#39;&#39;。我得到的错误是:

enter image description here

如何按DATE值常量过滤FireDAC查询?

1 个答案:

答案 0 :(得分:1)

FireDAC的Filter属性实现不支持DBMS功能,但为您提供了自己的表达式引擎。

使用FireDAC将字符串常量转换为DATE属性表达式中的Filter类型值可以像使用CONVERT宏函数或{d}转义序列一样简单。然后表达式引擎为您选择一个特定于DBMS的转换函数。

尝试使用其中一个过滤器(所有变体的日期字符串格式为 yyyy-mm-dd ):

uses
  FireDAC.Stan.ExprFuncs;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with FDQuery do
  begin
    { disable filter }
    Filtered := False;
    { filter with date escape sequence (is nicely readable) }
    Filter := 'DATA_NAS < {d 1996-07-29}';
    { filter with CONVERT macro function without function escape sequence }
    Filter := 'DATA_NAS < {CONVERT(''1996-07-29'', DATE)}';
    { filter with CONVERT macro function with function escape sequence }
    Filter := 'DATA_NAS < {fn CONVERT(''1996-07-29'', DATE)}';
    { enable filter }
    Filtered := True;
  end;
end;