0

I have the following sql query:

SELECT nickname, name from users;

Some users have name but don't have nickname and some have nickname but do not have name. I want to have sql query that checks if user have present name or nickname and return only present one. Here is how the result should look like:

Name
-------------
user_1_nickname
user_2_name
user_3_nickname
user_4_name

Is there way to do that in PostgreSQL?

1
  • If a user has both, which one to chose? Commented Aug 30, 2017 at 10:12

2 Answers 2

3

Use coalesce?

select coalesce(name, nickname) -- this prioritises name over nickname
from users;
Sign up to request clarification or add additional context in comments.

Comments

2

Give it a try with coalesce. It could be what you need.

Coalesce returns the first not null argument in the list, so you can do something like:

coalesce(nickname, name) and then where nickname is null, name is returned.

https://www.postgresql.org/docs/current/static/functions-conditional.html

Comments

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.