Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation in MySQL

People also ask

How concatenate varchar in MySQL?

CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.

What is the use of concat in MySQL?

MySQL CONCAT() Function The CONCAT() function adds two or more expressions together.

Can we use || in MySQL?

The || , operator is a nonstandard MySQL extension. As of MySQL 8.0. 17, this operator is deprecated; expect support for it to be removed in a future version of MySQL. Applications should be adjusted to use the standard SQL OR operator.


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.


Try:

select concat(first_name,last_name) as "Name" from test.student

or, better:

select concat(first_name," ",last_name) as "Name" from test.student

Use concat() function instead of + like this:

select concat(firstname, lastname) as "Name" from test.student

That's not the way to concat in MYSQL. Use the CONCAT function Have a look here: http://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat


Apart from concat you can also use concat_ws (concatenate with separator):

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

This function has the added benefit of skipping null values.

See https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws