w3resource logo


MySQL concat ws function

MySQL CONCAT() function

rating Average rating 8 out of 10. Total 71 users rated.

<<PreviousNext>>

Description

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.
  • Returns a binary string, if the arguments include any binary strings.
  • If the argument is numeric, it is converted to its equivalent nonbinary string form.
  • Returns NULL if any argument is NULL.

Syntax

CONCAT (string1, string2,…)

Arguments

Name Description
string1 First string to be joined.
string2 Second string to be joined. Up to N number of strings can be specified this way.

MySQL Version : 5.6

MySQL CONCAT Function Video

Examples of MySQL CONCAT() function

One argument :

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

Two or more arguments :

mysql> SELECT CONCAT('w3resource','.','com');
+--------------------------------+
| CONCAT('w3resource','.','com') |
+--------------------------------+
| w3resource.com                 |
+--------------------------------+
1 row in set (0.00 sec)

One of the arguments is NULL :

mysql> SELECT CONCAT('w3resource','.','com',NULL);
+-------------------------------------+
| CONCAT('w3resource','.','com',NULL) |
+-------------------------------------+
| NULL                                |
+-------------------------------------+
1 row in set (0.02 sec)

Numeric argument :

mysql> SELECT CONCAT(102.33);
+----------------+
| CONCAT(102.33) |
+----------------+
| 102.33         |
+----------------+
1 row in set (0.00 sec)

For quoted strings, concatenation can be performed by placing the strings next to each other :

mysql>SELECT 'w3resource' '.' 'com';
+----------------+
| w3resource |
+----------------+
| w3resource.com |
+----------------+
1 row in set (0.00 sec)

Pictorial representation of MySQL CONCAT() function

pictorial representation of MySQL CONCAT function

Example of MySQL CONCAT() function on columns

The following MySQL statement will add values of pub_city column with values of country column of publisher table placing a '-->' between them.

SELECT CONCAT(pub_city,'--> ',country)
FROM publisher; 

Sample table : publisher



Output

MySQL CONCAT() EXAMPLE

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>example-concat-function - php mysql examples | w3resource</title> </head> <body> <?php echo "<h2>list of publisher's city and country with 'publisher's city--->country' format : </h2>"; echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>"; echo "<tr style='font-weight: bold;'>"; echo "<td width='250' align='center'>Publisher's city</td>"; echo "</tr>"; include("../dbopen.php"); $result = mysql_query('SELECT CONCAT(pub_city,"--> ",country) FROM publisher'); while($row=mysql_fetch_array($result)) { echo "<tr>"; echo "<td align='center' width='200'>" . $row['CONCAT(pub_city,"--> ",country)'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> </body> </html>

View the example in browser

All String Functions

MySQL String Functions, slide presentation



We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.

<<PreviousNext>>