> I filter a database using the following code taking dates from two
> datetime pickers.
> 
>  DateFilter := 'Solddate >= ''' + Datetostr(dates.First.datetime) + '''';
>  DateFilter := DateFilter + ' AND Solddate <= ''' + 
> Datetostr(dates.last.datetime);
> items.filter := datefilter;
> 
> The problem is that it does not filter right.
> I set shortdateformat to dd/mm/yyyy and it worked better but it is still
> incorrect when selections are made from multipule months.

1. The standard SQL dateformat is MM/DD/YYYY. (Although I'm not sure about
   the separator - ORACLE seems to like '-').

2.  use FormatDateTime('MM/DD/YYYY',TheDateTimePicker.DateTime)  rather than
   DateToStr so that you don't have to change the ShortDateFormat globally.

3. On annoying SQL implementations that decide to localise the date or have different
   date separators try a parameterised query.

  with Query.Create(nil) do try
    DatabaseName := MyDatabase.DatabaseName;
    SQL.Add('Select Spam from SpamTable where StartDate>=:StartDate and 
EndDate<=:EndDate');
    Params.ParamByName('StartDate').AsDateTime := int(StartPicker.DateTime);
    Params.ParamByName('EndDate').AsDateTime := int(EndPicker.DateTime);
    Open;
    while not EOF do begin
       ..
       Next;
    end;
  finally
    Free;
  end;

The 'int' is to chop of the floating portion of a TDateTime which contains the time.

--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to