0

Here is my dummy Schema in MYSQL:
A column: which is a nullable string
B column: which is a string but its not null

enter image description here
I only want to select A columns but when facing a null, I want it to be replaced with the B column.
How can I do that?
this is my desired output
enter image description here

3 Answers 3

3

coalesce() or

SELECT CASE WHEN A IS NOT NULL THEN A ELSE B END AS value FROM tablename;
Sign up to request clarification or add additional context in comments.

Comments

2

Use coalesce():

select coalesce(a, b) as a
from t;

Comments

0

Or use ifnull, the sound of which makes it easy to remember. Note that it only works for 2 columns/values

select ifnull(a, b) as a
from t;

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.