MySQL query to update string field by concatenating to it?



For concatenating a string field, use CONCAT() function. Let us first create a table −

mysql> create table DemoTable    -> (    -> SequenceId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentId) values('STU'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentId) values('STU1'); Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+------------+-----------+ | SequenceId | StudentId | +------------+-----------+ | 1 | STU | | 2 | STU1 | +------------+-----------+ 2 rows in set (0.00 sec)

Here is the query to update string field by concatenating to it −

mysql> update DemoTable -> set StudentId=concat(StudentId,'-','101'); Query OK, 2 rows affected (0.14 sec) Rows matched: 2 Changed: 2 Warnings: 0

Let us check all the records once again from the table −

mysql> select *from DemoTable;

Output

+------------+-----------+ | SequenceId | StudentId | +------------+-----------+ | 1 | STU-101 | | 2 | STU1-101 | +------------+-----------+ 2 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:26+05:30

953 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements