2

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;
2
  • Just use 1 and 0 values. Commented Sep 19, 2016 at 10:06
  • 2
    @sagi: 1 and 0 aren't boolean values, those are numbers Commented Sep 19, 2016 at 21:57

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

the result of the query is important to me because I use it in my php code with a library, the result must be true / false without double quotes
@Nawalel I haven't use the PHP bindings of postgres but it should handle the first query correctly. If it doesn't try the third one, which converts the booleans to 0/1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.