5

I want to replace all values which has a NULL value with the the value in another column on the same row.

I have tried different sql statements without any success.

Regards

3
  • Post (add to your question) your best effort so far. Commented Feb 28, 2017 at 20:11
  • Do you mean you want to update your table in the way you describe, or that you want to perform such a replacement in the results of a query? Commented Feb 28, 2017 at 20:12
  • What is one of the different SQL statements that you have already tried? Commented Feb 28, 2017 at 20:41

2 Answers 2

14

If you want to update, do something like this:

update t set col1 = col2
where col1 is null

If you just want to do selection, use most databases support coalesce:

select coalesce(col1, col2) from t;
Sign up to request clarification or add additional context in comments.

2 Comments

what if col2 is null itself
At least in Postgresql: If all arguments are null, the COALESCE function will return null
0

If you want to Update the value from same column

UPDATE A
SET A.Col1 = A.Col2
From TableA A
Where A.Col1 IS NULL

If you want to update the value from Other table then Try this:

Update A
SET A.Column1 = B.Column1
From TableA A
INNER JOIN TableB B
ON A.SomeID = B.SameID
WHERE A.Column1 IS NULL

You can update the value by using join and update query.

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.