mysqlsqlreplacecoalesceifnull

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


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 ?


Solution

  • 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;