tags:

views:

75

answers:

3

if i get a null table cell in my results, is there a way to convert it to 0? but just for the results? not actually modifying the data?

+3  A: 

There is the COALESCE method which return the first non-null parameter, in your case :

COALESCE(field, 0)

But you can use this if you want more :

COALESCE(field1, field2, 0)
Colin Hebert
+4  A: 

You don't mention which DBMS you're using, but at least in SQL Server (and probably others), you can use COALESCE.

SELECT COALESCE(null_column, 0) AS null_column FROM whatever;

COALESCE goes through the list of values you give it, and returns the first non-null value.

Helgi Hrafn Gunnarsson
+1: `COALESCE` is ANSI, supported by SQL Server 2000+, Oracle 9i+, MySQL 4.1+, dunno the version of PostgreSQL or SQLite...
OMG Ponies
MySQL is mentioned in the title. I added a tag for it.
RedFilter
I think every 3VL DB with a SQL interface supports `coalesce()`. Case and point, [it was in MySQL 3.23](http://www.norrnod.se/norrnod/dokumentation/mysql/3.23.45/manual_toc.html#Comparison_Operators) which is about as close to the bottom of the barrel as you can get.
Evan Carroll
+1  A: 

MySQL:

SELECT COALESCE(Mycolumn, 0);
Sjoerd