18

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

46

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
15
update `whatever` set `null_column` = null where null_column = 0;
1
  • Not working for me in case of decimals and strict mode Oct 27, 2017 at 9:37
7

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

1
3

I used

UPDATE userDetails set fame=0 where fame IS NULL;

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.