Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql turn null into zero [duplicate]

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?

like image 991
maverick Avatar asked Sep 17 '11 04:09

maverick


People also ask

How do you change NULL to zero in PostgreSQL?

How do I replace nulls with zeros instead. SELECT ct. * INTO ct3 FROM crosstab( 'SELECT account_number, attr_name, sub FROM products ORDER BY 1,2', 'SELECT DISTINCT attr_name FROM attr_names ORDER BY 1') AS ct( account_number text, Attr1 integer, Attr2 integer, Attr3 integer, Attr4 integer, ... )

IS NULL THEN 0 in PostgreSQL?

In PostgreSQL, NULL means no value. In other words, the NULL column does not have any value. It does not equal 0, empty string, or spaces.

How do I convert NULL to zero in SQL?

Use IFNULL or COALESCE() function in order to convert MySQL NULL to 0. Insert some records in the table using insert command. Display all records from the table using select statement.

How do I use coalesce in PostgreSQL?

In PostgreSQL, the COALESCE function returns the first non-null argument. It is generally used with the SELECT statement to handle null values effectively. Syntax: COALESCE (argument_1, argument_2, …); The COALESCE function accepts an unlimited number of arguments.


1 Answers

select coalesce(max(column), 0) from mytable;  
like image 97
Mark Byers Avatar answered Sep 29 '22 10:09

Mark Byers