MySQL Forums
Forum List  »  Newbie

Re: CONCAT names with condition
Posted by: Rick James
Date: July 20, 2014 11:41PM

This might work:
CONCAT_WS(' ', first_name, middle_name, last_name)
if middle_name is NULL when it is missing.

mysql> SELECT CONCAT_WS(' ', 'aaa', NULL, 'bbb');
+------------------------------------+
| CONCAT_WS(' ', 'aaa', NULL, 'bbb') |
+------------------------------------+
| aaa bbb                            | -- NULL vanishes
+------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT_WS(' ', 'aaa', '', 'bbb');
+----------------------------------+
| CONCAT_WS(' ', 'aaa', '', 'bbb') |
+----------------------------------+
| aaa  bbb                         | -- empty string leaves the unwanted space
+----------------------------------+
1 row in set (0.00 sec)

Options: ReplyQuote


Subject
Written By
Posted
Re: CONCAT names with condition
July 20, 2014 11:41PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.