0

When i try to search both column, there is no filter. i want to list 5 and 30.05.2016 in dbgrid.

adoquery1.Close;
adoquery1.SQL.CLEAR;
adoquery1.SQL.Add('select * FROM Sheet11 ');
adoquery1.SQL.Add('where field9 like :blm and field12 like :blm1');
adoquery1.Parameters.ParamByName('blm').Value:='5';
adoquery1.Parameters.ParamByName('blm1').Value:='30.05.2016';
adoquery1.Open;
1
  • What is the type of field9? Commented Aug 5, 2016 at 8:24

2 Answers 2

1

So what is exactly the question there ? Using parameters is right, one better not use string splicing due to all kinds of fragility and unexpected side effects - http://bobby-tables.com

And use proper data types!

adoquery1.SQL.Text := 'select * FROM Sheet11 ' +
                      'where ( field9 = :_p_field9 ) ' + 
                      '  and ( field12 = :_p_field12 )';
with adoquery1.Parameters do begin
   ParamByName('_p_field9').AsInteger := 5;
   ParamByName('_p_field12').AsDateTime := EncodeDate( 2016, 05, 30 );
end;
adoquery1.Open;
Sign up to request clarification or add additional context in comments.

5 Comments

This is a better solution than mine (now deleted).
@Dsm well, maybe it is, maybe it is not. I still don't get what even is the problem here?..
well your solution is certainly better than mine. As to what is the problem - my - I am not an expert in SQL, but don't date fields need to be surrounded by quotation marks? I would expect the original query to generate "select * from sheet11 where field9 like 5 and field 10 like 30.05.2016". But maybe I am wrong...
@Dsm, well the topicstarter did not word his specific problem, so we can only wander in the myst blindguessing. There are many POTENTIAL problems and which one bit his low parts - we can only guess unless he would come down to us and spill it out :-(
However with dates - yes, it should be presented as string literal, quoted, and here we go - different database engines would demand different quotation marks, different ad hoc patterns of string representation, and some of those would not have implicit auto-typecast, requiring explicit conversion string to date. that all CAN be his problem, or CAN be not it. We can only guess :-(
1

You've got a pretty bad SQL error. LIKE does not work for any types other than strings (CHAR, VARCHAR, etc.). It does not work for numbers or dates. You're looking for = instead, for exact matches, or BETWEEN if you want something between two values.

This should work for you:

  adoquery1.Close;
  adoquery1.SQL.CLEAR;
  adoquery1.SQL.Add('select * FROM Sheet11 ');
  adoquery1.SQL.Add('where field9 = :blm and field12 = :blm1');
  adoquery1.Parameters.ParamByName('blm').Value:= 5; // Notice no quotes
  adoquery1.Parameters.ParamByName('blm1').Value:= '2016-05-30'; // Note no extra quotes
  adoquery1.Open;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.