I am getting NULL
values in the results of an operation in MySQL.
Is there a way to convert the NULL
values into the value 0?
I am getting NULL
values in the results of an operation in MySQL.
Is there a way to convert the NULL
values into the value 0?
Yes, by using
COALESCE
.COALESCE goes through the list of values you give it, and returns the first non-null value.
If you messed up and have NULLs in existing table layout and want zeros, here is solution:
There is the
COALESCE
method which return the first non-null parameter, in your case :But you can use this if you want more :
I am adding this answer because no one mentioned
IFNULL
functionYou can use IFNULL
IFNULL
will return column's value (if it has something other thanNULL
) otherwise second parameter passed (in this case0
).MySQL: