2

I have the following query:

SELECT id, first_name from users;

I want to represent first_name column as a boolean. If user have first_name it will be true, if not it will be false. How can I do that in PostgreSQL?

2 Answers 2

3

Just test for not null:

SELECT id, first_name is not null  as has_first_name
from users;

If you want to consider an empty string ('') as "no first name as well, you can use:

SELECT id, nullif(first_name,'') is not null  as has_first_name
from users;
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT id, 
       CASE WHEN first_name IS NULL THEN 'false' ELSE 'true' END
FROM users;

2 Comments

Nitpick: 'false' is not a boolean, it's a character value
@a_horse_with_no_name you are right, that is 'true'. on the other hand, these values are implicitly converted to boolean if need.

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.