Replace nulls values in sql using select statement?

You have a lot of options for substituting NULL values in MySQL:

CASE

select case 
    when fieldname is null then '123' 
    else fieldname end as fieldname 
from tablename 

COALESCE

select coalesce(fieldname, '123') as fieldname 
from tablename 

IFNULL

select ifnull(fieldname, '123') as fieldname 
from tablename 

There is a statement called IFNULL, which takes all the input values and returns the first non NULL value.

example:

select IFNULL(column, 1) FROM table;

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull


I think you're looking for the IFNULL function IFNULL(field, 0) will return a 0 when the field returns null

Tags:

Mysql

Sql