Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Consulta sql con rango de fechas (https://www.clubdelphi.com/foros/showthread.php?t=7146)

jgutti 05-02-2004 15:37:04

Consulta sql con rango de fechas
 
Hola al foro

Tengo una tabla paradox con campos mes y año

- Quiero filtrar toda la información entre el 11/2003 y el 01/2004

¿Como puedo realizar esta consulta?

gracias por la ayuda

JGutti

marcoszorrilla 05-02-2004 16:14:18

Código:

procedure TForm1.Button1Click(Sender: TObject);
var
dIni,dFin:Tdate;
begin
dIni:=Date - 365;
dFin:=Date - 300;

Query1.Close;
Query1.Sql.Clear;

Query1.Sql.Add('Select * from Factura ');
Query1.Sql.Add('Where fecha >='+QuotedStr(FormatDateTime('mm/dd/yyyy',dIni)));
Query1.Sql.Add('and fecha <='+ QuotedStr(FormatDateTime('mm/dd/yyyy',dfin)));

Query1.Open;
end;

Un Saludo.

sitrico 05-02-2004 17:09:59

Otra opción:

Select * From Tabla
Where ano*100+mes >= 200311 and ano*100+mes <= 200401

dannymunuera 26-02-2004 18:40:06

consulta de tiempo
 
Aquí pienso que te doy respuesta, es un procedimiento que realiza hace tiempo, lo mando completo y cojan lo que les sirva.

Var
Str1:String;
var1 :Variant;
WPuesto :Boolean;
begin
{filtrado de la base de datos }
OldEvento :=CBoxEventos.ItemIndex;
OldResp :=CBoxResponsable.ItemIndex;
WPuesto :=False;
With ModuloDatos do
Begin
MyQuery.Active :=False;
MyQuery.SQL.Clear;
MyQuery.SQL.Add('Select * from AcInd a');
If CBResponsable.Checked And ( CBoxResponsable.Text <> '' ) Then
Begin
var1 :=CBoxResponsable.text;
Str1 :='Where a.RESP =''' + VarToStr( Var1 ) + '''';
MyQuery.SQL.Add( Str1 );
WPuesto :=True;
End;
If CBEventos.Checked And ( CBoxEventos.Text <> '' ) Then
Begin
var1 :=CBoxEventos.text;
If Not WPuesto Then
Begin
Str1 :='Where a.EVENTOE =''' + VarToStr( Var1 ) + '''';
WPuesto :=True;
End
Else
Str1 :='And a.EVENTOE =''' + VarToStr( Var1 ) + '''';
MyQuery.SQL.Add( Str1 );
End;
If CBEstado.Checked And ( CBoxEstado.Text <> '' ) Then
Begin
var1 :=CBoxEstado.text;
If Not WPuesto Then
Begin
Str1 :='Where a.ESTADO =''' + VarToStr( Var1 ) + '''';
WPuesto :=True;
End
Else
Str1 :='And a.ESTADO =''' + VarToStr( Var1 ) + '''';
MyQuery.SQL.Add( Str1 );
End;
If FSelecc.ItemIndex = 0 Then
Begin
If CBFinicial.Checked Then
Begin
MyQuery.Params.CreateParam(ftdate, 'FromDate', ptInputOutput);
MyQuery.Params.ParambyName('FromDate').AsDate :=FechaInicial.Date;
If Not WPuesto Then
Begin
Str1 :='Where ( a.FECHAE >= :FromDate )';
WPuesto :=True;
End
Else
Str1 :='And ( a.FECHAE >= :FromDate )';
MyQuery.SQL.Add( Str1 );
End;
If CBFFinal.Checked Then
Begin
MyQuery.Params.CreateParam(ftInteger, 'ToDate', ptInputOutput);
MyQuery.Params.ParamByName('ToDate').AsDate :=FechaFinal.Date;
If Not WPuesto Then
Begin
Str1 :='Where ( a.FECHAE <= :ToDate )';
WPuesto :=True;
End
Else
Str1 :='And ( a.FECHAE <= :ToDate )';
MyQuery.SQL.Add( Str1 );
End;
End
Else
Begin
If CBFinicial.Checked Then
Begin
MyQuery.Params.CreateParam(ftdate, 'FromDate', ptInputOutput);
MyQuery.Params.ParambyName('FromDate').AsDate :=FechaInicial.Date;
If Not WPuesto Then
Begin
Str1 :='Where ( a.FECHAC >= :FromDate )';
WPuesto :=True;
End
Else
Str1 :='And ( a.FECHAC >= :FromDate )';
MyQuery.SQL.Add( Str1 );
End;
If CBFFinal.Checked Then
Begin
MyQuery.Params.CreateParam(ftInteger, 'ToDate', ptInputOutput);
MyQuery.Params.ParamByName('ToDate').AsDate :=FechaFinal.Date;
If Not WPuesto Then
Begin
Str1 :='Where ( a.FECHAC <= :ToDate )';
WPuesto :=True;
End
Else
Str1 :='And ( a.FECHAC <= :ToDate )';
MyQuery.SQL.Add( Str1 );
End;
End;
MyQuery.SQL.Add('Order by a.COD, a.RESP, a.EVENTOE, a.ESTADO');
MyQuery.Active :=True;
LbCant.Caption :=IntToStr( MyQuery.RecordCount );
End;

Cualquier aclaración tratare de darle respuesta, siempre que sea específico


Nuria 26-02-2004 19:23:01

Otra manera...
 
Hola!

Código:

Query1.Close;
Query1.Sql.Clear;

Query1.Sql.Add('Select * from Factura ');
Query1.Sql.Add('Where fecha between :prifec and :ultfec);
Query1.ParamByName('prifec').AsDate := '01/11/2003';
Query1.ParamByName('ultfec').AsDate := '01/01/2004';

Query1.Open;

Saludos!

eduarcol 26-02-2004 19:51:54

Me parece el de Nuria un ejemplo muy sencillo pero una pequeña acotacion:
En la linea : Query1.ParamByName('ultfec').AsDate := '01/01/2004';
coloca: Query1.ParamByName('ultfec').AsDate := '31/01/2004';

asi te toma tambien los del mes 01


La franja horaria es GMT +2. Ahora son las 20:59:05.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi