19

I want to replace 0's in mysql table with 'NULL'. I have read that querying the following way would replace 'NULL' with 0

SELECT COALESCE(null_column, 0) AS null_column FROM whatever;

But how to the other way?

5 Answers 5

51

You can use NULLIF, which will return NULL if the value in the first parameter matches the value in the second parameter.

SELECT NULLIF(null_column, 0) AS null_column FROM whatever
Sign up to request clarification or add additional context in comments.

Comments

15
update `whatever` set `null_column` = null where null_column = 0;

1 Comment

Not working for me in case of decimals and strict mode
8

Just use an UPDATE query, it's way faster: UPDATE table SET value=NULL WHERE value=0.

1 Comment

'' is not the same as NULL
3

I used

UPDATE userDetails set fame=0 where fame IS NULL;

if it to work. Since = did not work for me.

Comments

0

I have been using this method to clean individual columns but I would like to see if I can update an entire table doing the same method

UPDATE table SET value=NULL WHERE value=0.

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.