How to return 0 instead of NULL with MySQL

0
Categories: MySQL
Posted on: 15th May 2009 by: Andrei

Just banged my head on this one today while working on a new project in C#.

Here’s how you can return 0 in case SUM() or any other math function returns NULL.


SELECT COALESCE((SUM(myfield)),0) FROM mytable;

COALESCE() returns the first non-null value, therefore if SUM() is null, then it will return the following 0.