PostgreSQL: How to Display Boolean Values as TRUE or FALSE


By default, PostgreSQL displays values from a Boolean column as t and f.

However, occasionally you may want to display the values as  true and false instead.

The easiest way to do so is by casting the Boolean column to text:

SELECT athleteID,
       team,
       all_star::text
FROM athletes;

This particular example will display the values from the Boolean column named all_star as either true or false.

The following example shows how to use this syntax in practice.

Example: How to Display Boolean Values as TRUE or FALSE in PostgreSQL

Suppose that we create the following table named athletes that contains information about various basketball players:

-- create table named 'athletes'
CREATE TABLE athletes (
  athleteID INTEGER PRIMARY KEY,
  team TEXT,
  all_star BOOLEAN
);

-- add rows to the table 
INSERT INTO athletes VALUES (0001, 'Mavs', true);
INSERT INTO athletes VALUES (0002, 'Mavs', true);
INSERT INTO athletes VALUES (0003, 'Cavs', false);
INSERT INTO athletes VALUES (0004, 'Heat', true);
INSERT INTO athletes VALUES (0005, 'Mavs', false);
INSERT INTO athletes VALUES (0006, 'Thunder', true);
INSERT INTO athletes VALUES (0007, 'Cavs', false);
INSERT INTO athletes VALUES (0008, 'Lakers', false);
INSERT INTO athletes VALUES (0009, 'Celtics', false);
INSERT INTO athletes VALUES (0010, 'Lakers', false);

-- view all rows in table
SELECT * FROM athletes;

 id |  team   | all_star 
----+---------+----------
  1 | Mavs    | t
  2 | Mavs    | t
  3 | Cavs    | f
  4 | Heat    | t
  5 | Mavs    | f
  6 | Thunder | t
  7 | Cavs    | f
  8 | Lakers  | f
  9 | Celtics | f
 10 | Lakers  | f

The all_star column contains the values t and f to indicate whether or not each player is an all-star player.

We can cast the all_star column to text to instead display these values as true and false:

SELECT athleteID,
       team,
       all_star::text
FROM athletes;

 athleteid |  team   | all_star 
-----------+---------+----------
         1 | Mavs    | true
         2 | Mavs    | true
         3 | Cavs    | false
         4 | Heat    | true
         5 | Mavs    | false
         6 | Thunder | true
         7 | Cavs    | false
         8 | Lakers  | false
         9 | Celtics | false
        10 | Lakers  | false

Notice that the values in the all_star column are now displayed as true and false.

You can also use the upper() function if you’d like to display the values as TRUE or FALSE:

SELECT athleteID,
       team,
       upper(all_star::text)
FROM athletes;

 athleteid |  team   | upper 
-----------+---------+-------
         1 | Mavs    | TRUE
         2 | Mavs    | TRUE
         3 | Cavs    | FALSE
         4 | Heat    | TRUE
         5 | Mavs    | FALSE
         6 | Thunder | TRUE
         7 | Cavs    | FALSE
         8 | Lakers  | FALSE
         9 | Celtics | FALSE
        10 | Lakers  | FALSE

We could also use the initcap() function to display the values as True and False:

SELECT athleteID,
       team,
       initcap(all_star::text)
FROM athletes;

 athleteid |  team   | initcap 
-----------+---------+---------
         1 | Mavs    | True
         2 | Mavs    | True
         3 | Cavs    | False
         4 | Heat    | True
         5 | Mavs    | False
         6 | Thunder | True
         7 | Cavs    | False
         8 | Lakers  | False
         9 | Celtics | False
        10 | Lakers  | False

Feel free to use whichever method you prefer to display the True and False values from a Boolean column in whatever format you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in PostgreSQL:

PostgreSQL: How to Count True and False Values
PostgreSQL: Count Occurrences of Each Value in Column
PostgreSQL: Count Number of Occurrences of Character in String

Leave a Reply