Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace 0 with null in mysql

Tags:

sql

null

mysql

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?

like image 789
ben Avatar asked Sep 07 '12 20:09

ben


2 Answers

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
like image 94
LittleBobbyTables - Au Revoir Avatar answered Sep 18 '22 23:09

LittleBobbyTables - Au Revoir


update `whatever` set `null_column` = null where null_column = 0;
like image 41
Hawili Avatar answered Sep 19 '22 23:09

Hawili