Ferhat Ergin Turan
Ferhat Ergin Turan

Reputation: 5

Delphi sql search with both column

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;

Upvotes: 0

Views: 604

Answers (2)

Ken White
Ken White

Reputation: 125651

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;

Upvotes: 1

Arioch 'The
Arioch 'The

Reputation: 16045

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;

Upvotes: 1

Related Questions