String concatenation in MySQL

MySQL is different from most DBMSs’ use of + or || for concatenation. It uses the CONCAT function:

SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student

There’s also the CONCAT_WS (Concatenate With Separator) function, which is a special form of CONCAT():

SELECT CONCAT_WS(' ', first_name, last_name) from test.student

That said, if you want to treat || as a string concatenation operator (same as CONCAT()) rather than as a synonym for OR in MySQL, you can set the PIPES_AS_CONCAT SQL mode.

Leave a Comment