In a Postgres database, I know how to use case statements to get only TRUE or FALSE for a boolean field but was wondering whether there is a cleaner way to set this up like changing any configuration in the db.
Update: in Postgres, a the boolean representation for true is not only TRUE but also: yes, y, on, "t". So, I am inserting values with value TRUE. However, the SELECT command returns those values as "t".
3 Answers 3
I have found the root of this issue. Postgres is fine. It returns boolean as true and numbers as int except decimal type values.
The problem was I was using pg_connect in PHP and fetchAll of this library casts everything to string. I am using PDO now and everything is normal.
Lets close this thread.
-
1To "close" this question, you need to accept an answer. It's perfectly fine to accept your own answer.user1822– user18222019年11月15日 06:48:48 +00:00Commented Nov 15, 2019 at 6:48
if I understand you .. use this https://www.postgresql.org/docs/9.3/ddl-default.html
CREATE TABLE T (
b boolean not null default FALSE
);
on the other hand postgres still allows for more values that will be translated to true or false (Table of literal values for true and false according to http://www.postgresqltutorial.com/postgresql-boolean/)
True False
true false
‘t’ ‘f‘
‘true’ ‘false’
‘y’ ‘n’
‘yes’ ‘no’
‘1’ ‘0’
so you probably can't enforce true / false - on other hand a select will deliver true / false as you want
-
I have updated my question but yes, that's what I want to avoid. If I insert TRUE, I don't expect to get "t".Luis Arriojas– Luis Arriojas2019年11月13日 12:36:14 +00:00Commented Nov 13, 2019 at 12:36
-
The
t
output completely depends on the SQL client you are usinguser1822– user18222019年11月13日 12:36:40 +00:00Commented Nov 13, 2019 at 12:36 -
@eagle275 I created my table using the same command you posted. Inserted a TRUE value but the Select command returns "t". So, when I send this to Angular app, it expects to receive a boolean value, not a string.Luis Arriojas– Luis Arriojas2019年11月13日 12:41:47 +00:00Commented Nov 13, 2019 at 12:41
-
2@LuisArriojas: Postgres does return real boolean value. The
t
you are seeing is just the default output format applied by e.g.psql
. If you retrieve the value (correctly) in a programming language, you will get a proper boolean value. Don't confuse output formatting with the actual column valueuser1822– user18222019年11月13日 12:43:42 +00:00Commented Nov 13, 2019 at 12:43 -
That's what I accounted for - given its some years back that I supported a team mate with postgre but I remember that the boolean coliumn beautifully corresponded to a boolean value in my chosen programming language (JAVA at that time) - but Angular as a webservice might blur the lineseagle275– eagle2752019年11月13日 12:45:49 +00:00Commented Nov 13, 2019 at 12:45
There is probably something that I don't understand about your question but given a table with a boolean attribute like:
create table T ( b boolean not null );
You can only insert True, False. Under what circumstances would you need a CASE expression?
Edit:
reading your comments above, I guess you are interested in a textual representation of t/f. Not sure if it fits your needs but you can cast it to text:
select b::text from T
If you want it to be TRUE/FALSE I guess you will have to:
select upper(b::text) from T
Perhaps a function would be cleaner?
CREATE FUNCTION showbool(boolean) RETURNS text
AS $$ SELECT UPPER(CAST(1ドル AS text)) $$
LANGUAGE SQL;
select showbool(b) from T
-
Boolean in postgres is much more than only true and false. You can use": True False, true, false, ‘t’, ‘f‘, ‘true’, ‘false’, 'y’, ‘n’, ‘yes’, ‘no’, ‘1’, ‘0’, on, off.Luis Arriojas– Luis Arriojas2019年11月13日 12:38:14 +00:00Commented Nov 13, 2019 at 12:38
-
I would say that 'no' etc are casted to booleanLennart - Slava Ukraini– Lennart - Slava Ukraini2019年11月13日 13:14:11 +00:00Commented Nov 13, 2019 at 13:14
value > 42