Skip to content


Convert NULL to Zero '0' in MySQL

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Convert NULL to Zero '0' in MySQL

Postby Web Guru » October 28th, 2008, 4:26 am

Sometimes you need to sum up a column using SUM() function. This function return 'NULL' if for a specific column/field no record is found but in order to manipulate our results within query we need 0 instead of NULL. Same will be the case with other group functions like AVG, etc.

To overcome this issue, MySQL provides a function COALESCE() to convert a NULL value into 0. You can use this function as COALESCE(SUM(field_name), 0) .. this will return you 0 if no value is found.
User avatar
Web Guru
 
Posts: 94
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan

Return to Board index

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests