有一个表COST里面有个DATE字段TYPE是DATE型。数据格式是2007-02-15
我现在想在DELPHI实现按照月份提取数据,比如在COMBOBOX的下拉列表中选择2就能查询出所有2月份的的数据。
我原来写的代码是
  query1.DatabaseName :='d:\';
  query1.Close;
  query1.SQL.Clear;
  query1.SQL.Add('select * from cost where month(date)='+''''+combobox2.text+'''');
  query1.Open;
运行的时候报错 invalid use of keyword token:month(date)='2'//因为我选择的是2月份。
但是在SQL中运行SELECT * from cost where month(date)='2'能执行,也能查处数据。请大家帮帮忙吧,急
数据是写在VF表中,是DATE型

解决方案 »

  1.   

    试着改成这一句:
    query1.SQL.Add('select * from cost where month(date)='+QuotedStr(comboBox2.text));
      

  2.   

    format('select * from cost where month(date)=%d ',[strtoint(combobox2.text)]);
      

  3.   

    不行,还是要报错
    一样的是invalid use of keyword token:month(date)='2
      

  4.   

    query1.Close;
      query1.SQL.Clear;
      query1.SQL.Add('select * from cost where TYPE between '+quotedStr('2007-'+combobox2.text+'1')+' and '+QuotedStr('2007-'+IntToStr(StrtoInt(combobox2.text)+1)+'-1');
      query1.Open;
      

  5.   

    刚才少打了一个字符
    query1.Close;
      query1.SQL.Clear;
      query1.SQL.Add('select * from cost where TYPE between '+quotedStr('2007-'+combobox2.text+'-1')+' and '+QuotedStr('2007-'+IntToStr(StrtoInt(combobox2.text)+1)+'-1');
      query1.Open;
      

  6.   

    select * from cost where month([date])='+comboBox2.text你这个报错的原因有可能是date这个字段被解析成一个关键字了.
    还有你这样的查询有什么意思呀.呵呵,月份等于2月,年份居然不考虑 .呵呵
      

  7.   

    这样的话数据有几年的呢那你这个统计可有问题了,可以通过Month(日期字段)取出月份出来再三比较
      

  8.   

    可以先用一个函数判断出该年该月的第一天和最后一天,然后用between and 啊
      

  9.   

    好简单的哦
    where date >= 该月的第一天 and date < 下月的第一天
      

  10.   

    不要判断该月有多少天,不要使用 month函数。