Concatenating two strings in MySQL with space?

MySQLMySQLi Database

For this, you can use concat() function from MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20),
   -> Subject varchar(100)
   -> );
Query OK, 0 rows affected (17.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name,Subject) values('John','MySQL');
Query OK, 1 row affected (1.19 sec)

mysql> insert into DemoTable(Name,Subject) values('Chris','SQL Server');
Query OK, 1 row affected (0.88 sec)

mysql> insert into DemoTable(Name,Subject) values('Robert','MongoDB');
Query OK, 1 row affected (2.62 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+----+--------+------------+
| Id | Name   | Subject    |
+----+--------+------------+
| 1  | John   | MySQL      |
| 2  | Chris  | SQL Server |
| 3  | Robert | MongoDB    |
+----+--------+------------+
3 rows in set (0.00 sec)

Here is the query to concatenate two strings with space −

mysql> update DemoTable
   -> set Subject=concat(Name,' ',Subject);
Query OK, 3 rows affected (0.38 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check all records from the table −

mysql> select *from DemoTable;

Output

+----+--------+------------------+
| Id | Name   | Subject          |
+----+--------+------------------+
| 1  | John   | John MySQL       |
| 2  | Chris  | Chris SQL Server |
| 3  | Robert | Robert MongoDB   |
+----+--------+------------------+
3 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 13:06:01
Advertisements