1

I need a help with some challenge

How can I (using sql script for PostgreSQL) copy data from column A to B if A is NULL and if B is null as well copy data from column C?

So if A is null copy data from B but if B is also Null copy from column C. All data are in same table.

Is it possible?

3
  • Show us some sample table data, both before and after versions. (As formatted text, not images.) Commented Feb 28, 2020 at 10:12
  • I don't have them yet. In general column A is new and pretty much empty, script should copy string value from column B to A, but if B is NULL then should copy data from column C but only if A is NULL. This is just a future case so column A doesn't exist yet Commented Feb 28, 2020 at 10:15
  • Just make something up, a few rows having different null /not null combinations. Commented Feb 28, 2020 at 10:19

1 Answer 1

3

Perhaps COALESCE is what you want here with an update:

UPDATE yourTable
SET A = COALESCE(B, C)
WHERE A IS NULL;

This logic would update records with a NULL value for A with values from B, or if the B values also be NULL, from C.

Sign up to request clarification or add additional context in comments.

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.