Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output "yes/no" instead of "t/f" for boolean data type in PostgreSQL

How do I make a query that would return yes/no instead of t/f (true/false)?

Current solution is:

SELECT credit_card_holders.token IS NOT NULL AS premium

I found a Ruby solution: Rails (or Ruby): Yes/No instead of True/False

But would rather do it in PostgreSQL if possible.

like image 911
Serge Vinogradoff Avatar asked Mar 10 '15 08:03

Serge Vinogradoff


People also ask

How do you represent a boolean in PostgreSQL?

PostgreSQL provides the standard SQL type boolean; see Table 8-19. The boolean type can have several states: "true", "false", and a third state, "unknown", which is represented by the SQL null value. Leading or trailing whitespace is ignored, and case does not matter.

What is the difference between Bool and boolean in PostgreSQL?

PostgreSQL Boolean is a simple data type that we have used to represent only the structure of true or false data or values. PostgreSQL will support the SQL99 defined Boolean data type of SQL standard; Boolean is also known as “bool”, bool is an alias of Boolean data type in PostgreSQL.

Does PostgreSQL have boolean?

PostgreSQL provides the standard SQL type boolean ; see Table 8.19. The boolean type can have several states: “true”, “false”, and a third state, “unknown”, which is represented by the SQL null value.

What does <> mean in PostgreSQL?

<> is the standard SQL operator meaning "not equal". Many databases, including postgresql, supports != as a synonym for <> . They're exactly the same in postgresql.


2 Answers

Ended up with this:

(case when credit_card_holders.token IS NOT NULL then 'Yes' else 'No' end) AS premium
like image 189
Serge Vinogradoff Avatar answered Sep 24 '22 15:09

Serge Vinogradoff


by creating custom types also you can achieve this, see the following example

create table foo (id int,xid int);
insert into foo values (1,2),(2,3);

we have following data

id xid 
-- --- 
1  2   
2  3   

and the following select statements returns boolean value.

select exists(select * from foo where xid=4);

exists
boolean
------
f

select exists(select * from foo where xid=3);

exists
boolean
------
t

ok now we need to return YES and NO instead of t and f, so we can create a custom type like below

create type bool2yesno as enum ('YES','NO'); --or whatever you want 'yes','no'.

and create a function to convert boolean to the created custom type i.e bool2yesno

create function convert_bool_to_bool2yesno(boolean)
  returns bool2yesno
  immutable
  strict
  language sql
as $func$
  select case $1
    when false then 'NO'::bool2yesno
    when true  then 'YES'::bool2yesno
  end
$$;

now create a cast for the newly created type

create cast (boolean as bool2yesno )
  with function convert_bool_to_bool2yesno(boolean)
  as assignment;

now again try the select statement

select exists(select * from foo where xid=4)::bool2yesno ;

exists 
bool2yesno 
----------
NO     

select exists(select * from foo where xid=3)::bool2yesno ; 
exists 
bool2yesno 
---------- 
YES 

Reference :
CREATE TYPE
CREATE CAST
CREATE FUNCTION

like image 43
Vivek S. Avatar answered Sep 21 '22 15:09

Vivek S.