MySQL Concatenation Operator

I do not know the concatenation operator for MySQL.

I tried this code to combine:

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

But that did not work. Which operator should be used to concatenate strings?

+33
sql mysql concatenation
Mar 26 '13 at 6:58
source share
6 answers

You used concatenation like ORACLE. MySQL should be

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

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

+39
Mar 26 '13 at 7:01
source share

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

+57
Jul 16 '14 at 9:33
source share

The MySQL CONCAT function is used to concatenate two strings to form a single string. Try the following example:

 mysql> SELECT CONCAT('FIRST ', 'SECOND'); +----------------------------+ | CONCAT('FIRST ', 'SECOND') | +----------------------------+ | FIRST SECOND | +----------------------------+ 1 row in set (0.00 sec) 

To better understand the CONCAT function, consider the employee_tbl table, which has the following entries:

 mysql> SELECT CONCAT(id, name, work_date) -> FROM employee_tbl; +-----------------------------+ | CONCAT(id, name, work_date) | +-----------------------------+ | 1John2007-01-24 | | 2Ram2007-05-27 | | 3Jack2007-05-06 | | 3Jack2007-04-06 | | 4Jill2007-04-06 | | 5Zara2007-06-06 | | 5Zara2007-02-06 | +-----------------------------+ 
+14
Mar 26 '13 at 7:01
source share

You can simply use the CONCAT keyword to concatenate strings. You can use it as

 SELECT CONCAT(vend_name,'',vend_country) FROM vendors ORER BY name; 
+1
Jul 31 '18 at 10:52
source share

The advantage of using concat is that you can pass columns of different data types and concat string representations.

  SELECT concat('XXX', 10.99, 'YYY', 3, 'ZZZ', now(3)) as a; 

Exit


-----
XXX10.99YYY3ZZZ2018-09-21 15: 20: 25.106

+1
21 Sep '18 at 19:21
source share

Before executing a query using channels as a concatenation operator, you must set up the pipeline as a concatenate.

-one
Jan 14 '18 at 19:29
source share



All Articles