Postgresql turn null into zero

SqlPostgresqlNullMax

Sql Problem Overview


> Possible Duplicate:
> SELECT max(x) is returning null; how can I make it return 0?

When I execute

select max(column) from mytable;

and my table has no rows, it returns null. How can I amend this select statement so it will return zero?

Sql Solutions


Solution 1 - Sql

select coalesce(max(column), 0) from mytable; 

Solution 2 - Sql

Try:

SELECT coalesce(max(column), 0) myalias FROM mytable;

Solution 3 - Sql

Do either of these work?

    select coalesce(max(foo),0) from bar
    coalesce((select max(foo) from bar),0)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaverickView Question on Stackoverflow
Solution 1 - SqlMark ByersView Answer on Stackoverflow
Solution 2 - SqlPhil SandlerView Answer on Stackoverflow
Solution 3 - Sqlvol7ronView Answer on Stackoverflow