Author Topic: asString x asInteger, when value is zero.  (Read 1868 times)


  • Newbie
  • Posts: 3
asString x asInteger, when value is zero.
« on: December 18, 2017, 08:46:09 pm »
In the database Postgresql, I defined the field as:

ball numeric not null,

When one register, have value '0' (zero), the property asString return a empty string,
instead of returning a string with value '0'.

In the component TSqlQuery, when try access the valor as String, so:

   ball: string;
sqlRegister := TSqlQuery.Create;
ball := sqlRegister.FieldByName('ball').asString,

asString, returned a empty string instead of a string with value zero.

I know I should use AsInteger because the value of the field in the table is numeric, however, I am populating the data in a STRINGGRID.
However, I found that the behavior of asString was to convert the value zero to a string with the value zero, however, it converts with an empty string, behaving as if the field 'ball' had a null value, which is not correct in this case.

The Workaround who I did was:
ball := IntToStr(sqlRegister.FieldByName('ball').asInteger);

My question is, Why asString should not return an empty string instead of the value '0', when the value of the register field is 0???
« Last Edit: December 18, 2017, 09:01:32 pm by zuibaf »


  • Full Member
  • ***
  • Posts: 125
Re: asString x asInteger, when value is zero.
« Reply #1 on: December 19, 2017, 02:10:03 am »
Is it possible that you have a setting in your database which converts 0 to null? I don't know PostgresSQL, but a lot of RDBMS systems have these sort of settings buried in the configuration!
Try getting the value with AsInteger, then convert to string as a check.
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)


  • Sr. Member
  • ****
  • Posts: 296
Re: asString x asInteger, when value is zero.
« Reply #2 on: December 19, 2017, 05:30:57 am »
Zero may be trated as null so the empty string it's correct.

To solve this, you can try StrToIntDef :

Code: Pascal  [Select][+][-]
  1. function StrToIntDef(  const S: ;  Default: Integer ): Integer;

Pass 0 (zero) as default.

Or simple use a conditional

Code: Pascal  [Select][+][-]
  1. if sqlRegister.FieldByName('ball').asString='' then ball:=0;
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7.2.6 - Sqlite 3.32.3


TinyPortal © 2005-2018