Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL concatenation operator

I don't know concatenation operator for MySQL.

I have tried this code for concatenation:

SELECT vend_name || ' (' || vend_country || ')' FROM Vendors ORDER BY vend_name; 

But it didn't work. Which operator should I use to concatenate strings?

like image 589
user2201462 Avatar asked Mar 26 '13 06:03

user2201462


People also ask

What is the concatenation operator in MySQL?

MySQL CONCAT() function is used to add two or more strings. There may be one or more arguments. Returns the string that results from concatenating the arguments. Returns a nonbinary string, if all arguments are nonbinary strings.

Can you use || to concat in MySQL?

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. Better answer, explains that MySQL doesn't use concatenation operators.

How do I concatenate a query 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 operator for concatenation?

The concatenation operator is a binary operator, whose syntax is shown in the general diagram for an SQL Expression. You can use the concatenation operator ( || ) to concatenate two expressions that evaluate to character data types or to numeric data types.


2 Answers

|| is the ANSI standard string concatenation operator, supported by most databases (notably not MS SQL Server). MySQL also supports it, but you have to SET sql_mode='PIPES_AS_CONCAT'; or SET sql_mode='ANSI'; first.

like image 66
200_success Avatar answered Sep 22 '22 14:09

200_success


You were using ORACLE type of concatenation. MySQL's Should be

 SELECT CONCAT(vend_name, '(', vend_country, ')') 

Call the CONCAT() function and separate your values with commas.

like image 44
codingbiz Avatar answered Sep 21 '22 14:09

codingbiz