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.   

    建议用edit控件把你的sql语句捕捉出来,到sqlserver的query里边运行一下,就可以清楚的发现哪里出错了
      

  3.   

    我觉得你的流程有问题:你要重新调整下流程,我认为问题出在:你的年月查询中,没有 where而日查询中有 where
      

  4.   

    like一下就可以了,怎么这么麻烦
    ================================
    严重同意
    但是这样在月和日的查询的时候会不会出问题?
      

  5.   

    教你一个办法,呵呵`~用个MEMO取你在运行后的sql.text在放到SQL里去看看哪里错了。改了就好。
    还有就是''''这个看起来太麻烦了建议你用#39来代替。我都是这么做的。
      

  6.   

    Format函数格式化掉,把年月日字符串加起来,条件为空的就用%代替
    比如查 2004 的
    YearStr='2004';MonthStr='%';Datestr='%'
    查 2004-01 的
    YearStr='2004';MonthStr='01';Datestr='%'s:=format('where convert(char(12),orderdate,120) like '''+%s-%s-%s'''',[YearStr,MonthStr,Datestr]);