Reputation: 341
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?
Upvotes: 19
Views: 64641
Reputation: 1
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.
Upvotes: 0
Reputation: 9574
I used
UPDATE userDetails set fame=0 where fame IS NULL;
if it to work. Since =
did not work for me.
Upvotes: 3
Reputation: 1342
Just use an UPDATE
query, it's way faster: UPDATE table SET value=NULL WHERE value=0
.
Upvotes: 8
Reputation: 32680
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
Upvotes: 49