Nawal el
Nawal el

Reputation: 45

return a boolean using Postgres

How to return a boolean true/false instead of t/f from a field of string type in Postgres.

SELECT (CASE WHEN status = 'active' THEN true ELSE false END)::boolean AS status 
FROM table;

Upvotes: 2

Views: 5138

Answers (1)

redneb
redneb

Reputation: 23920

t and f is just how psql prints booleans, it shouldn't be important. Also, you can simplify your query as follows:

SELECT status = 'active' AS status FROM table;

If you really want to get text for the column status, do the following:

SELECT (status = 'active')::text AS status FROM table;

If you want to get a number instead (0 for false, 1 for true), do this:

SELECT (status = 'active')::integer AS status FROM table;

Upvotes: 3

Related Questions