Search code examples
delphidelphi-7

Operator not applicable to this operand when I try to make filter based on date


I am kind of new to Delphi and Query and before I posted this question, I searched everywhere that related to my problem but it seems I was unable to find anything that could help me solve my problem.

I found this error message that operator not applicable to this operand type with this code:

SQL.Text:='SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE' 
  +QuotedStr('%'+cxButtonEdit1.Text+'%') 'AND MONTH(Tgl)'= 
  +QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date)')+ 'AND YEAR(Tgl)'= 
  +QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date));

How do I solve this error?


Solution

  • You are missing a + operator, and have some quotes mixed up. Instead of:

    SQL.Text:='SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE' 
      +QuotedStr('%'+cxButtonEdit1.Text+'%') 'AND MONTH(Tgl)'= 
      +QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date)')+ 'AND YEAR(Tgl)'= 
      +QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date));
    

    I presume you mean

    SQL.Text:='SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE' 
      +QuotedStr('%'+cxButtonEdit1.Text+'%') +' AND MONTH(Tgl)='
      +QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date))+ ' AND YEAR(Tgl)=' 
      +QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date));
    

    Note also that you must stop building queries this way. You are opening yourself up to SQL injection attacks. Use parameterised queries instead.