0

I am trying to get three columns as a string but for some reason I only get NULL.

this is the code:

SELECT CONCAT(name, ' ', city, ' ', country) FROM places   

the table places contains the three columns name, city and country and it is not empty, however, it can contain NULL in some of the columns.

The result:

NULL
NULL
NULL,
etc

I tried the answer from this post MySQL CONCAT returns NULL if any field contain NULL and it doesn't work for me.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Abdel Hidalgo
  • 109
  • 1
  • 8

2 Answers2

3

Can you try with CONCAT_WS()

SELECT CONCAT_WS(' ', name, city, country) FROM places 
Arulkumar
  • 12,966
  • 14
  • 47
  • 68
1

You probably want to use something like COALESCE(name, '') to convert a null string to an empty string. Since null means unknown, mysql will not assume it means empty string. Unknown concatenated with anything is still unknown. You have to tell it explicitly to treat it as empty string and that is what the coalesce is for.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159