CONCAT multiple fields to a single field, single spaced

牧云@^-^@ 提交于 2019-11-30 17:06:48

MySQL has CONCAT_WS - concatenate with separator

CONCAT_WS(' ', first, middle, maiden, last);

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws

As pointed out by andr below, make sure any concatenated fields contain NULL and 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 NULL field. You'd have to do a COALESCE(column, '') wrapper around each nullable column.

empty string solution TRIM(BOTH ' ' FROM CONCAT_WS(' ', first, middle, maiden, last))

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_trim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!