一样的问题在:http://community.csdn.net/Expert/topic/3716/3716926.xml?temp=.6074335
begin
    //日
    if Indx=0 then
    begin
      SQLStr:=SQLStr+'where '+LUFieldName+' = To_Date('+
        ''''+FormatDateTime('yyyy-mm-dd',dtLUDate.Date)
        +''''+','+''''+'yyyy-mm-dd'+''')';
    end;
    //月
    if Indx=1 then
    begin
      SQLStr:=SQLStr+' and '+LUFieldName+' Between To_Date('+
        ''''+FormatDateTime('yyyy-mm',dtLUDate.Date)+'-01'+''''+','+
        ''''+'yyyy-mm-dd'+''''+')'+
        ' and To_Date('+
        ''''+FormatDateTime('yyyy-mm',dtLUDate.Date)+
        IntToStr(DaysInMonth(dtLUDate.Date))+''+''''+','+
        ''''+'yyyy-mm-dd'+''''+')';
    end;
    //年
    if Indx=2 then
    begin
     SQLStr:=SQLStr+' and '+LUFieldName+' Between To_Date('+
        ''''+FormatDateTime('yyyy',dtLUDate.Date)+'-01-01'+''''+','+
        ''''+'yyyy-mm-dd'+''''+')'+
        ' and To_Date('+
        ''''+FormatDateTime('yyyy',dtLUDate.Date)+'-12-31'+''''+','+
        ''''+'yyyy-mm-dd'+''''+')';
    end;
这是代码的一部分,是根据年或月或日查询,为什么根据年和月查询时提示:语句为正确结束。
谢谢各位了

解决方案 »

  1.   

    楼主问题还没解决?
    说说你的目的
    试试其他的方法啊用like吧
      

  2.   

    参考:
    var
      dt_Date : TDateTime;
      indx : Integer;
      SQLStr : String;
    begin
      SQLStr := 'select * FROM sysobjects Where 1=1';
      case Indx of
        0:  SQLStr := SQLStr+' and datediff(day,crdate,'''+DateToStr(dt_Date)+''')';
        1:  SQLStr := SQLStr+' and datediff(month,crdate,'''+DateToStr(dt_Date)+''')';
        2:  SQLStr := SQLStr+' and datediff(year,crdate,'''+DateToStr(dt_Date)+''')';
      end;
      with ADOQuery1 do
      begin
        SQL.Clear;
        SQL.Text;
        Open;
      end;
    没调试
    你自己看正确不?crdate是表的日期字段
      

  3.   

    错了
    应该这样 
    var
      dt_Date : TDateTime;
      indx : Integer;
      SQLStr : String;
    begin
      indx := 2;
      dt_Date:=Now;
      SQLStr := 'select * FROM sysobjects Where 1=1';
      case Indx of
        0:  SQLStr := SQLStr+' and datediff(day,crdate,'''+DateToStr(dt_Date)+''')=0';
        1:  SQLStr := SQLStr+' and datediff(month,crdate,'''+DateToStr(dt_Date)+''')=0';
        2:  SQLStr := SQLStr+' and datediff(year,crdate,'''+DateToStr(dt_Date)+''')=0';
      end;
      ShowMessage(SQLStr);
      with ADOQuery1 do
      begin
        SQL.Clear;
        SQL.Text:=SQLStr;
        Open;
      end;
    end;