- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements