

A common usage of the CONCATWS() function is to create a comma-delimited list. If you just use the CONCAT() function, you’d have no separator (unless you explicitly added a separator as an argument between each string argument). For example, if you use PostgreSQL or Oracle, you have to use. This is one of the differences between MySQL and T-SQL (SQL Server, Azure). In MySQL, the CONCATWS() function allows you to add a separator to concatenated strings. MySQL string concatenation is cleaner in comparison with other database management systems.

CONCAT (String1,String2,String3.) For example, to display the book’s name. To display the contents of two columns or more under the name of a single column, you can use the MySQL Concat function with the following syntax : 1. If the separator itself is a NULL value, the concatenation operation will return NULL.Įxample: SELECT CONCAT_WS(NULL,'Auckland', NULL, 'New Zealand') AS Location MySQL String concatenation allows you to append one string to the end of another string. CONCAT(expression1, expression2, expression3. Note: Also look at the CONCATWS() function. If any of the arguments is a NULL value, MySQL will skip that value and its separator, but it will still process the others.Įxample: SELECT CONCAT_WS(', ','Auckland', NULL, 'New Zealand') AS Location The CONCAT() function adds two or more expressions together. Here’s an example of retrieving data from a database, and combining two columns into one, separated by a comma: SELECT CONCAT_WS(', ', city.Name, country.Name ) AS Location SELECT CONCAT_WS(' - ','Paris', 'France') AS Location Here’s the same example as the previous one, except this one uses a different separator. There’s nothing to say that the separator must be a comma. Here’s an example: SELECT CONCAT_WS(',','Sydney', 'Australia') AS Location Īnd you can add a space in there if you want: SELECT CONCAT_WS(', ','Sydney', 'Australia') AS Location If you just use the CONCAT() function, you’d have no separator (unless you explicitly added a separator as an argument between each string argument).Ī common usage of the CONCAT_WS() function is to create a comma-delimited list. Trying to use it as a concatenation operator may produce unexpected results if you don’t enable it as a pipe concatenation operator first.In MySQL, the CONCAT_WS() function allows you to add a separator to concatenated strings. By default, MariaDB treats || as a synonym for the OR logical operator.

Note that the pipe concatenation operator first needs to be enabled before you can use it in this manner. In this case, I appended a space to the first argument. This example uses the concatenation operator ( ||): SELECT 'Agent ' || 47 Result: Agent47 The Pipe Concatenation Operator ( ||) If you don’t want the numeric value to be converted to its equivalent binary string form, you can explicitly cast it before the concatenation operation.Įxample: SELECT CONCAT('Agent', CAST(47 AS char)) Spaces can be added, either by adding the space to the existing string, or by concatenating including a third argument that consists solely of a space: SELECT Any numeric value is converted to its equivalent binary string form (this is in contrast to MySQL, which returns a nonbinary string). Although this is a string function, it can handle numeric (and binary string) arguments. The CONCAT() function concatenates its arguments. Use the pipe concatenation operator ( ||), which concatenates its operands.īelow are examples of each.Use the CONCAT() function, which concatenates its arguments.Here are two ways to concatenate strings and numbers in MariaDB:
