Edg*_*uin 1 delphi datetime sql-server-2005 delphi-2010
我正在使用SQL Server 2005 Management Studio Express和Delphi 2010. Fecha_hora= Date_Time is smalldatetime.
我的日期格式是 dd/mm/yyy
我表中的日期保存如下:
08/01/2013 11:22:00 a.m.
Run Code Online (Sandbox Code Playgroud)
我在德尔福有这个问题,知道在一段时间内销售额会更高; 天/月,在这种情况下,我在2013年1月8日的同一天进行测试:
conect.Q_total_hora.Active:=false;
conect.Q_total_hora.SQL.Clear;
conect.Q_total_hora.SQL.Add('select datepart(hh, fecha_hora) as Hora, sum(Total) as Venta, a.tipo as Tipo');
conect.Q_total_hora.SQL.Add('from ventas v join articulos a on v.id_articulo=a.id_articulo');
conect.Q_total_hora.SQL.Add('where tipo='+char(39)+DBLUCB_tipo.Text+char(39)+' and cast(Convert(varchar(10), fecha_hora, 112) as datetime) between'+char(39)+DateToStr(DateTimePicker_fecha1.Date)+char(39)+ 'and'+char(39)+DateToStr(DateTimePicker_fecha2.Date)+char(39));
conect.Q_total_hora.SQL.Add('group by datepart(hh,fecha_hora), a.tipo order by datepart(hh,fecha_hora) ');
conect.Q_total_hora.Active:=true;
Run Code Online (Sandbox Code Playgroud)
我使用,cast(Convert(varchar(10), fecha_hora, 112) as datetime)因为我在互联网上发现,这样我只能检索日期,没有时间在日期之间检索数据.
在DateTimePickers我选择08/01/20132013年1月8日
我使用备忘录查看查询 memo1.Text:=conect.Q_total_hora.Text;
我收到的查询是:
select datepart(hh, fecha_hora) as Hora, sum(Total) as Venta, a.tipo as Tipo
from ventas v join articulos a on v.id_articulo=a.id_articulo
where tipo='Burrito Grande' and cast(Convert(varchar(10), fecha_hora, 112) as datetime) between'08/01/2013'and'08/01/2013'
group by datepart(hh,fecha_hora), a.tipo order by datepart(hh,fecha_hora)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我在SQL Server Mgmt Studio中运行此查询时,它返回值,但不是在Delphi中,而在Delphi中,如果我将DateTimePickersto 的值设置01/08/2013为2013年8月1日,则返回值08/01/2012.
据我所知(并且我不太了解......)当我向SQL Server发送查询时,就像我在SQL中编写它...为什么如果我将日期08/01/2013作为字符串发送它没有返回任何东西?
先感谢您.我在数据库方面不是很好,我在互联网上找的大多数东西都是^^
您可以避免所有这些问题,并使用参数化查询编写更清晰的代码.
试试这个
conect.Q_total_hora.Active:=false;
conect.Q_total_hora.SQL.Clear;
conect.Q_total_hora.SQL.Add('select datepart(hh, fecha_hora) as Hora, sum(Total) as Venta, a.tipo as Tipo');
conect.Q_total_hora.SQL.Add('from ventas v join articulos a on v.id_articulo=a.id_articulo');
conect.Q_total_hora.SQL.Add('where tipo=:tipo and fecha_hora between :fecha1 and :fecha2');
conect.Q_total_hora.SQL.Add('group by datepart(hh,fecha_hora), a.tipo order by datepart(hh,fecha_hora) ');
conect.Q_total_hora.Prepared:=True;
conect.Q_total_hora.ParamByName('tipo').AsString := DBLUCB_tipo.Text;
conect.Q_total_hora.ParamByName('fecha1').AsDateTime := DateTimePicker_fecha1.Date;
conect.Q_total_hora.ParamByName('fecha2').AsDateTime := DateTimePicker_fecha2.Date;
conect.Q_total_hora.Open;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |