I would use CONCAT_WS. For instance:
SELECT CONCAT_WS(' ', NULL, 'First', NULL, 'Last', NULL);
This will return the string "First Last" with no spaces anywhere except one CONCAT_WS, between two strings that are not NULL.
The first argument to CONCAT_WS is the glue that appears between non-NULL values.
In your case, it will be:
SELECT CONCAT_WS(' ', forename, initials, surname) AS name FROM users;
From here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws
Edit: this only works in MySQL.
source
share