I’m trying to concatenate a first middle maiden and last name fields and use that to update a single field which is named fullname
For each user any combination of these 4 fields can be filled. From 0 to all 4.
However I also need a single space between each name (not multiple spaces).
UPDATE nameTable SET fullname = CONCAT(first, middle, maiden, last);
MySQL has
CONCAT_WS– concatenate with separatorhttp://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws
As pointed out by
andrbelow, make sure any concatenated fields containNULLand not an empty string ('') otherwise you will get a double space in the output.Fiddle: http://sqlfiddle.com/#!2/1fe83/1
Further Application
Be careful therefore if in the future you use this function to make a small CSV list, because you won’t get the comma for a
NULLfield. You’d have to do aCOALESCE(column, '')wrapper around each nullable column.