Params and operator Like

Discussion of open issues, suggestions and bugs regarding IBDAC (InterBase Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
iveres
Posts: 10
Joined: Tue 06 Mar 2012 08:00
Location: Croatia, BJ
Contact:

Params and operator Like

Post by iveres » Mon 15 Jul 2013 19:09

Hi!
Now I migrate from sdac to ibdac (returning to Firebird :D at new employee).
In dynamic building query sql on some condition I need to use AddWhere method that adds some condition and one param.
And It works.

But, it doesn't work with param.

Usually I use:
someqry.AddWhere ( 'somefield = :pMyValue');
...
someqry.ParamByName('pMyValue').Value = '10';

and RecordSet returns some record by condition.

But, if I use:

someqry.AddWhere ( 'somefield Like '':pMyValue%''');
...
someqry.ParamByName('pMyValue').Value = '10';

I got error...


Working solution is:

someqry.AddWhere ( 'somefield Like :pMyValue');
...
someqry.ParamByName('pMyValue').AsString = '10%';

and then Recordset returning couple records expected by this like condition.

But, I wanna % at same place as Like - in AddWhere string, not in param.

How to get "where statment" in FinalSql as: somefield Like ':pMyValue%' and ParamByName('pMyValue').Value = somevaluestring;

Do I need "mannulay" adding param and how to do this?

Thanks!

AndreyZ

Re: Params and operator Like

Post by AndreyZ » Tue 16 Jul 2013 07:20

Hello,

Using parameters (and macros), you should specify the '%' character in the parameter value.
If you want to use the '%' character in the SQL statement itself, you should replace it manually. Here is an example:

Code: Select all

begin
  someqry.SQL.Text := 'query text';
  someqry.AddWhere('somefield like ''$somefield%''');
  someqry.SQL.Text := StringReplace(someqry.SQL.Text, '$somefield', '10', [rfReplaceAll, rfIgnoreCase]);
  someqry.Open;
end;

iveres
Posts: 10
Joined: Tue 06 Mar 2012 08:00
Location: Croatia, BJ
Contact:

Re: Params and operator Like

Post by iveres » Tue 16 Jul 2013 21:35

Thanks!

AndreyZ

Re: Params and operator Like

Post by AndreyZ » Wed 17 Jul 2013 07:17

If any other questions come up, please contact us.

Post Reply