Page 1 of 1

FieldByName('fieldname').AsString not Returning entire field

Posted: Tue 28 Oct 2008 20:21
by gymwalker
I have a field in an SQL DB that is defined as CHAR(100)...I am using the SDAC components (still on trial)...and I am performing the following:

TempStr := datamodule.tablename.fieldbyname('fieldname').asstring;

As I said this field is defined as CHAR(100)...however, its only returning data up to the first NULL it finds in the field...not the entire 100 bytes. How do I get this to function to return all 100 bytes of the field and not stop at the first NULL value in the field that it runs into?

I know...this is not good database management...however, this DB came from an old COBOL system where they did a lot of large fields with several sub-fields in them so they didn't have to keep expanding the DB.

In COBOL this is pretty easy to diffine in a buffer...but I'm struggling with accessing all 100 bytes of data in Delphi 6.

Any recomomendation?

Thanks,
Jim

Posted: Wed 29 Oct 2008 11:18
by Dimon
To keep any bytes (including NULL) in a field you should use the BINARY field type instead of CHAR.

FieldByName('fieldname').AsString not Returning entire field

Posted: Wed 29 Oct 2008 16:23
by gymwalker
I don't have any control over the database definition...I'm stuck with the CHAR(100)...can I define a binary field (target) in Delphi and manipulate the data there? If so, how would I define a 100 byte binary data field?

Thanks in advance!
Jim

Posted: Thu 30 Oct 2008 12:20
by Dimon
You can change the field type with ALTER TABLE ... ALTER COLUMN statement.
In other way you can change your SELECT query using CAST statement for change of field type, like this:

Code: Select all

  SELECT CAST(FieldName AS BINARY(100)) FieldAlias, ...