Norman
Norman

Reputation: 6365

Working with CONCAT and NULL values

Why is my entire string blank when I output data using php doing:

select concat(name, desc) as output from reports where type=2

And the data in the table is:

Name Desc
a    desc a
b    null
c    null

Output using php:

a desc a
blank space
blank space

If I didn't use concat, the output is as usual. I'm using concat so I can output everything in one variable when using php.

Upvotes: 1

Views: 60

Answers (1)

John Woo
John Woo

Reputation: 263713

you can use CONCAT_WS()

concat_ws('', name, `desc`)

or with COALESCE()

concat(name, COALESCE(`desc`,''))

Upvotes: 1

Related Questions