user1234 user1234 - 4 months ago 10
MySQL Question

Issue with CONCAT() mysql?

I have a

table
and i am running a
query
and fetching full name from table like this

SELECT CONCAT(f_name , " - ", l_name ) as fullname FROM user


It wokrs fine but when i have no value in
l_name
which is
null
by default

It doesn't give
f_name
instead it gives me
NULL

Answer

You can use CONCAT_WS:

SELECT CONCAT_WS(' - ', f_name,  l_name)  as fullname FROM  user

concat will return null if a value is null, concat_ws will just skip null values.

Comments