MySQL, IFNULL(), COALESCE() on String not replacing

kiltek picture kiltek · Jan 18, 2012 · Viewed 14.2k times · Source

My statement should replace every empty title_column with 'no name', but it doesn't:

SELECT COALESCE(main_table.title_column, 'no name') AS title
FROM main_table;

IFNULL() behaves the same way.

What am I doing wrong ?

Answer

newtover picture newtover · Jan 18, 2012

COALESCE and IFNULL substitute only NULL values, your table seem to contain empty strings:

SELECT
  COALESCE(NULLIF(main_table.title_column, ''), 'no name') AS title
FROM main_table;