Postgresql turn null into zero

68,011

Solution 1

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

Solution 2

Try:

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

Solution 3

Do either of these work?

  • select coalesce(max(foo),0) from bar
    
  • coalesce((select max(foo) from bar),0)
    
Share:
68,011
maverick
Author by

maverick

Updated on July 01, 2020

Comments

  • maverick
    maverick almost 4 years

    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?

  • Ajay Kumar
    Ajay Kumar over 6 years
    Worked like a charm!!!!