views:

1252

answers:

2

I have a query in Delphi using DBExpress TSQLQuery that looks like so

ActiveSQL.sql.add('SELECT * FROM MYTABLE where MYFIELD=(:AMYFIELD) ');

ActiveSQL.ParamByName('AMYFIELD').AsString    := 'Some random string that is to long for the field';

ActiveSQL.Open;

If I run it, when it executes the open command I get the following exception

in class TDBXError with message 'arithmetic exception, numeric overflow or string truncation'.

This is caused by the string in AMYFIELD been longer then the tables field length, MYFIELD is Varchar(10), If I trim it down to a shorter string it works OK, and if I add the string directly into the SQL like so

  ActiveSQL.sql.add('SELECT * FROM MYTABLE where MYFIELD="Some random string that is to long for the field" ');

it works OK, i.e. does not complain about the truncation, now if this was an insert/update I would want to know about the truncation , but as its just been used for a search I would like to stop it.

Is there any way I can tell DBExpress that it is OK to truncate my strings? or is there a workable work around for this

I would like to avoid having to add something like

l_input := copy(l_input,0,fieldLength-1);

as looks messy and would make maintaining the code harder.

I am using Delphi 2007 with Firebird 2 via the interbase driver if that helps?

UPDATE:

@Erick Sasse it looks like your right, I found the error message on the firebird FAQ site http://www.firebirdfaq.org/faq79/

@inzKulozik the LeftStr works fine, although I cannot get ActiveSQL.ParamByName('AMYFIELD').Size to work , but this still seams messy to me, and harder to maintain.

I have also seen a method that adds substr to the SQL: something like

select * from mytable where myname = substr(:MYNAME,0,10)

Again looks harder to maintain, Ideally I would like a Firebird/ DBExpress config setting that fixes this problem, but until I can find one I'll go with inzKulozik's solution and hope the table structure does not change to much.

+1  A: 
l_input := copy(l_input,**0**,fieldLength-1);

You can't copy substring from position 0!

Try this:

l_input := LeftStr(l_input, fieldLength);

or

ActiveSQL.ParamByName('AMYFIELD').AsString := LeftStr('Some random string that is to long for the field', ActiveSQL.ParamByName('AMYFIELD').Size);

or

with ActiveSQL.ParamByName('AMYFIELD') do
  AsString := LeftStr('Some random string that is to long for the field', Size);
inzKulozik
I didnt know about leftStr, ill give it a go monday
Re0sless
+1  A: 

This error message is from Firebird, not DBX. I think DBX sends parameters to the database exactly the way you enter it, so it should be Firebird job to truncate it.

Erick Sasse