Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

Delphi TFDQuery - Filter for multiple expressions and YearOf

I'm trying to set up a filter with the actual year and month. Database is MSSQL.

I added DateUtils to my uses. How can I set up the filter expression to use the YearOf() function?

How can I add more than one line filter expression? Is this possible?

This is working:

with FDQuery2 do begin
  Filtered := False;
  Filter := '"Posting Year" = 2021';  //+ 'Posting month = January'
  Filtered := True;
end;

Unfortunately, this returns me an error:

with FDQuery2 do begin
  Filtered := False;
  Filter := '"Posting Year" = YearOf';  
  Filtered := True;
end;

image

How can I add a second filter expression?

UPDATE: I used now a little workaround to avoid much more work due to converting TDateTime, etc.

var
  YearFilter: string;

YearFilter := IntToStr(CurrentYear);
question from:https://stackoverflow.com/questions/65898683/delphi-tfdquery-filter-for-multiple-expressions-and-yearof

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I managed to get the filtering now correctly. To get the month of the year I have to declare a TDate variable. MonthOfTheYear returns me the month as Dword - for use in a filter expression it needs converting to a string via IntToStr.

Date is the function System.SysUtils.Date. http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.Date

 Date1 := Date;

We assign Date1 variable just the actual date with System.SysUtils.Date

var 
   Date1: TDate;
   YearFilter, MonthFilter: string;

       with FDQuery2 do begin
         Filtered := False;

         YearFilter := IntToStr(CurrentYear);
          Date1 := Date();
         MonthFilter := IntToStr(MonthOfTheYear(Date1));

         Filter := '"Posting Year" = ' + QuotedStr(YearFilter) + ' AND ' + '"Posting Month" = ' + QuotedStr(MonthFilter);

         Filtered := True;

       end;
     


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...