如何用query查找日期为某天的记录?

zhongyh 2002-03-01 08:38:20
我的代码如下:
begin
s:=datetostr(now);
query1.Close;
query1.SQL.Clear ;
query1.SQL.Add('select * from tabname where 日期='+s );
if not query1.Prepared then query1.Prepare ;
query1.ExecSQL ;
end;
运行时说表达式错。为什么?
...全文
112 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pipiyantai 2002-03-02
  • 打赏
  • 举报
回复
OutQuery.Close;
OutQuery.SQL.Clear;
OutQuery.SQL.Add('select * from OutTable ');
OutQuery.SQL.Add(' where (日期 <=:date1 ) ');
OutQuery.ParamByName('date1').Value:=formatdatetime('yy-mm-dd',datetimepicker1.Date);
xcp077 2002-03-02
  • 打赏
  • 举报
回复
首先,你先查一查你的数据库中的时间格式是什么?比如2002-03-02或2002。03。02?
然后如果是2002-03-02格式,用query1.SQL.Add('select * from tabname where 日期='+#39+FormatDateTime('yyyy-mm-dd',Now) +#39 );
注意格式就对了。否则不行。只gigi
caiyun 2002-03-02
  • 打赏
  • 举报
回复
用参数
zhongyh 2002-03-01
  • 打赏
  • 举报
回复
可以具体说说吗。
上面的方法还是不行。
??????
应该没有这么难吧???
hfyun 2002-03-01
  • 打赏
  • 举报
回复
得注意日期的格式。不同数据库中格式是不一样的,但如果用参数的形式传入会避免这种情况。
simonvc 2002-03-01
  • 打赏
  • 举报
回复
//SQL Server版对Access版也同样有用!
simonvc 2002-03-01
  • 打赏
  • 举报
回复
//Access版
begin
s:=datetostr(now);
query1.Close;
query1.SQL.Clear ;
//这条语句仅仅对Access有用,但用于SQL Server则不行
query1.SQL.Add('select * from tabname where 日期='+ '#' +
s + '#');
if not query1.Prepared then query1.Prepare ;
query1.Open;
end;

//SQL Server版
begin
s:=datetostr(now);
query1.Close;
query1.SQL.Clear; //这句一定要在前!!
with Query1 do
begin
with Parameters.AddParameter do
begin
Name := 'simon';
DataType := ftDate;
Direction := pdInput;
Value := s;
end;
end;
query1.SQL.Add('select * from tabname where 日期= :simon');
if not query1.Prepared then query1.Prepare ;
query1.Open;
end;

windindance 2002-03-01
  • 打赏
  • 举报
回复
query1.SQL.Add('select * from tabname where 日期='''+FormatDateTime('mm/dd/yyyy',Now) +'''' );
Liujc 2002-03-01
  • 打赏
  • 举报
回复
select * from tabname where 日期='mm/dd/yyyy'
zhongyh 2002-03-01
  • 打赏
  • 举报
回复
就是因为Query1.Open不行,我才改为Query1.ExecSql的。
结果还是不行。
踢踏 2002-03-01
  • 打赏
  • 举报
回复
query1.ExecSQL 改为
Query1.Open

830

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧