Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - TQuery .AsString to return 0 or 1 for Boolean field values

How can i get Delphi 7 to return a '0' or '1' when the FieldType of a TQuery descendant's Field is a ftBoolean? By default this returns 'TRUE' or 'FALSE', that is

Query1.Fields[0].AsString would return '0', not 'FALSE'

like image 226
Simon Avatar asked Jan 18 '23 16:01

Simon


2 Answers

Use

(Query1.Fields[0] as TBooleanField).DisplayValues := 'TRUE;FALSE';

to set a string in the form of 'TRUE;FALSE' (or '1;0'). This allows you to define what values AsString will return.

If you added the field in design time, and/or you got yourself a boolean field component, you can use that too, without the typecast:

Query1YourBooleanField.DisplayValues := 'TRUE;FALSE';

By the way, it's not the query that returns '0', not is it the query that 'is' ftBoolean. These are the fields in the query that represent fields in the table or query result set.

like image 89
GolezTrol Avatar answered Jan 21 '23 07:01

GolezTrol


Why not simple use Query1.Fields[0].AsInteger if you want a numerical representation for the boolean ? I think that should work ...

like image 25
Stefaan Avatar answered Jan 21 '23 07:01

Stefaan