Search code examples
sqlmysqlconcatenationsql-like

Multiple concat with like not showing the output in sql


I am using multiple concat with like but I am not getting all the output from the table. I am using below SQL query below in the database.

  select 
  company_id as ID, 
  concat(
    c.list_rank, '|||', c.company_name, 
    '|||', c.company_size, '|||', c.industry
  ) as post_content, 
  c.admin_approved from company22 c where company_name like ' % XYZ % ';

I am getting below output. Post content is showing null but I have all the data in the table

enter image description here


Solution

  • At least one of the values you're concatenating must be NULL. Since any operation involving NULL returns NULL, that makes the final result NULL.

    Use IFNULL() to replace the null values with an empty string.

    CONCAT_WS('|||', IFNULL(c.list_rank, ''), IFNULL(c.company_name, ''), IFNULL(c.company_size, ''), IFNULL(c.industry, '')) AS post_content
    

    I also use CONCAT_WS() to simplify joining several values with the same delimiter.