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.
MySQL CONCAT() Function The CONCAT() function adds two or more expressions together.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With