读书人

delphi中怎么操作数据库获得相应时间的

发布时间: 2012-02-14 19:19:19 作者: rapoo

delphi中如何操作数据库获得相应时间的数据
begin
y1:=strtoint(formatdatetime('yyyy',form2.DateEdit1.Date) );
y2:=strtoint(formatdatetime('yyyy',form2.DateEdit2.Date) );
m1:=strtoint(formatdatetime('m',form2.DateEdit1.Date) );
m2:=strtoint(formatdatetime('m',form2.DateEdit2.Date) );

yy1:=inttostr(y1);
yy2:=inttostr(y2);
mm1:=inttostr(m1);
mm2:=inttostr(m2);
if m1<10 then begin mm1:='0'+mm1;end;
if m2<10 then begin mm2:='0'+mm2;end;
ch1:=yy1+'-'+mm1+'-1'; //初始时间yyyy-mm-1(旬时间)
ch2:=yy2+'-'+mm2+'-3'; //最后时间yyyy-mm-3(旬时间)

ado1.Close;
ado1.SQL.Clear ;
sql:= 'select x_date,Quzhanhao,avg(MP),max(MAXP),min(MINP),avg(MT),max(MAXT),sum(R2020),count(1) from TBL_RiZiLiao where QuZhanHao='+iii+' and R2020>=1 and x_date>='''+ch1+''' and x_date<='''+ch2+''' group by x_date,QuZhanHao';
ado1.SQL.add(sql);
ado1.open;
为什么我时间选择2010-01-01到2010-02-28时候,只得到2月的3条数据,1月的数据为什么没有出现?

[解决办法]
为什么定义这么多变量,还要把日期拆来拆去的,你想干什么?
实际上:
ch1:=formatdatetime('yyyy-mm-1',form2.DateEdit1.Date)
ch2:=formatdatetime('yyyy-mm-3',form2.DateEdit2.Date)

没有数据,用showmessage(sql);看看条件符合不符合!!!!
[解决办法]
这个问题。就是format函数的用法了。

读书人网 >.NET

热点推荐