Jump to content
Analyste2023

search between two dates

Recommended Posts

hello Delphi geniuses, I hope you are well, I have a problem, how to filter the data between two dates, i.e. the user wants to display only the data from 06/01/2023 to 06/30/2023 for example, thank you for your help

image.thumb.png.4bc0e9ae72232afe8e28f7b67a3676e9.png

Share this post


Link to post
1 hour ago, Analyste2023 said:

hello Delphi geniuses, I hope you are well, I have a problem, how to filter the data between two dates, i.e. the user wants to display only the data from 06/01/2023 to 06/30/2023 for example, thank you for your help

 

No data to provide any help on, sorry. We need more detail. If your UI view is populated from a database query just use an appropriate condition in the WHERE clause of the query. Most database engines support the BETWEEN operator for dates; just make sure to formulate the bounds using parameters and not date literals (which are sensitive to date formats, locales for client and server, great potential to cause a mess).

  • Like 1

Share this post


Link to post

using "Between" dates:    

Quote

select * from Sales

where order_date BETWEEN  cast('xx/xx/xxxx' as date)  AND cast('xx/xx/xxxx' as date)

order by order_date

Using "PARAMetrizations" in your FDQuery for example:

 

image.png

 

procedure TForm1.Button1Click(Sender: TObject);
begin
  SalesTable.Close;
  SalesTable.ParamByName('DateBegin').AsDate := StrToDate('06/01/2023');
  SalesTable.ParamByName('DateEnd').AsDate   := StrToDate('06/30/2023');
  SalesTable.Open;
end;

 

Edited by programmerdelphi2k
  • Like 2

Share this post


Link to post

👍 programmerd2k response, and, with firedac, you can simplify his code to :

 

procedure TForm1.Button1Click(Sender : TObject)
begin
Salestable.open('',[DateFrom.Date,Dateto.Date]);
end;

 

Edited by Serge_G
  • Like 3

Share this post


Link to post
7 hours ago, Serge_G said:

, you can simplify his code to :

Serge, there are so many possibilities that we often forget the obvious... good times when there weren't so many options  😂

  • Like 1

Share this post


Link to post

bonjour a tous ,merci a vous de m'avoir aider  vos intervention m'en beaucoup aider dans l'avancement de mon projet je suis fier d'etre parmi vous et tres content d'utiliser delphi pour les applications windows

Share this post


Link to post
On 10/06/2023 at 15:11, PeterBelow said:

Aucune donnée pour fournir de l'aide, désolé. Nous avons besoin de plus de détails. Si votre vue d'interface utilisateur est remplie à partir d'une requête de base de données, utilisez simplement une condition appropriée dans la clause WHERE de la requête. La plupart des moteurs de base de données prennent en charge l'opérateur BETWEEN pour les dates ; assurez-vous simplement de formuler les limites en utilisant des paramètres et non des littéraux de date (qui sont sensibles aux formats de date, aux paramètres régionaux pour le client et le serveur, un grand potentiel de causer des dégâts).

je vous remercie pour votre intervention

Share this post


Link to post
16 hours ago, Analyste2023 said:

bonjour a tous ,merci a vous de m'avoir aider  vos intervention m'en beaucoup aider dans l'avancement de mon projet je suis fier d'etre parmi vous et tres content d'utiliser delphi pour les applications windows

Il existe un très bon forum en français https://www.developpez.net/forums/f15/environnements-developpement/delphi/ (mon pseudo SergioMaster)

Share this post


Link to post

If you use the TDateTimePicker component to select a date then take care of the Time part of the

component (see object inspector) because Delphi sneaky adds the current time when you drop

the component on the form.. If you have a time part also in the database table field then

be sure your SQL does the right query when doing things like <=, = , >= if the time part

of the component is set on 00:00:00. 

Share this post


Link to post
5 hours ago, Die Holländer said:

Delphi sneaky adds the current time when you drop

the component on the form.

for that, you has the "Date" property:

Quote

  DateTimePicker1.Date;
  CalendarPicker1.Date;
  DatePicker1.Date;

 

Share this post


Link to post

I always use DateOf when assigning a TDate to a  date field in a sql query. 

It's sometimes hard to know if somehow a time part was added to a date value. So just to be sure i do a lot of:
(especially when the TDate field/param is in a SQL select statement)

 

procedure DoMyUpdate(const AMyDateParameter : TDate);
begin
   qry.ParamByName('MyDateParameter').AsDate := DateOf(AMyDateParameter);
   ...
 end;

 


or if needed (eg in a loop) extract the Date value outside the loop:

 

procedure DoMyUpdate(const AMyDateParameter : TDate);
var
  myRealDateVar	: TDate;
begin
  myRealDateVar := DateOf(AMyDateParameter);
  while i<cnt do
  begin
    ....
    ....
    qry.ParamByName('MyDateParameter').AsDate := myRealDateVar;
   inc(i);
   .....
end;

 

Edited by mvanrijnen

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×