怎样用ADOQuery按一段时间(如:2008-01-01到2008-03-25)对时间字段进行查询?(100分)

  • 主题发起人 主题发起人 菜鸟黄
  • 开始时间 开始时间

菜鸟黄

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样用ADOQuery按一段时间(如:2008-01-01到2008-03-25)对时间字段进行查询?
 
select * from 表 where 日期字段>='2008-01-1' and 日期字段<='2008-03-25'
 
不行,我试过了~
 
提示:标准表达式中数据类型不匹配
 
你的数据库是SQLSERVER还是ORACLE?
 
select * from 表 where 日期字段>=2008-01-1 and 日期字段<=2008-03-25
没有报错,可是查询不到数据
 
啊~还是有问题啊~没有报错,可以查不出结果啊
 
我是这样写的~你们看一下吧
procedure TForm1.N7Click(Sender: TObject);
var
s,m,n:string;
year,month,day:word;
i:integer;
begin
i:=DayOfTheWeek(now);
decodedate(now,year,month,day);
if (length(inttostr(month))=2)and(length(inttostr(day))=2) then
begin
m:=inttostr(year)+'-'+inttostr(month)+'-'+inttostr(day-i+1);
n:=inttostr(year)+'-'+inttostr(month)+'-'+inttostr(day-i+7);
end;
if (length(inttostr(month))=2)and(length(inttostr(day))=1) then
begin
m:=inttostr(year)+'-'+inttostr(month)+'-0'+inttostr(day-i+1);
n:=inttostr(year)+'-'+inttostr(month)+'-0'+inttostr(day-i+7);
end;
if (length(inttostr(month))=1)and(length(inttostr(day))=2) then
begin
m:=inttostr(year)+'-0'+inttostr(month)+'-'+inttostr(day-i+1);
n:=inttostr(year)+'-0'+inttostr(month)+'-'+inttostr(day-i+7);
end;
if (length(inttostr(month))=1)and(length(inttostr(day))=1) then
begin
m:=inttostr(year)+'-0'+inttostr(month)+'-0'+inttostr(day-i+1);
n:=inttostr(year)+'-0'+inttostr(month)+'-0'+inttostr(day-i+7);
end;

ADOQuery6.Active :=False;
s:='select * from Cz_Main where Date>=';
s:=s+m+' and Date<=';
s:=s+n;
ADOQuery6.SQL.Text :=s;
ADOQuery6.Active :=True;
end;
 
ADOQuery6.Active :=False;
s:='select * from Cz_Main where Date>='''+ M +''' and Date<=''' + n + '''';
ADOQuery6.SQL.Text :=s;
ADOQuery6.Active :=True;
 
select * from 表 where convert(varchar(10), 日期字段, 120) between '2008-01-01' and '2008-03-25'
 
看你用什么数据库了
 
oracle:
select * from Cz_Main where to_char(Date1,'yyyy-mm-dd') >='2008-01-01' and to_char(Date1,'yyyy-mm-dd') <='2008-12-01'
 
问题解决了~
s:='select * from Cz_Main where Date>=';
s:=s+m+' and Date<=';
s:=s+n;
改为
s:='select * from Cz_Main where Date>=';
s:=s+'#'+m+'#'+' and Date<=';
s:=s+'#'+n+'#';
 
多人接受答案了。
 

Similar threads

回复
0
查看
848
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部