2
votes

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

2 Answers

3
votes

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;
1
votes
SELECT id, 
       CASE WHEN first_name IS NULL THEN 'false' ELSE 'true' END
FROM users;