
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
In MySQL, how to remove the specific prefix from entire column’s value and update them?
It can be done by applying TRIM() function on the column along with MySQL UPDATE statement. The example below will make it more clear.
Example
Suppose, we have a table ‘Employee’ which have the prefix ‘Dept.’ with all the values of Column ‘Department’ as follows −
mysql> Select * from Employee; +------+----------------+------------+----------------------+ | Id | Name | Address | Department | +------+----------------+------------+----------------------+ | 100 | Raman | Delhi | IT Dept. | | 101 | Mohan | Haryana | History Dept. | | 102 | Shyam | Chandigarh | ENGLISH Dept. | | 103 | Sukhjeet Singh | Patiala | Computer Engg. Dept. | | 104 | Bimal Roy | Calcutta | Computer Engg. Dept. | +------+----------------+------------+----------------------+ 5 rows in set (0.01 sec)
Now, the query below will remove the prefix ‘Dept.’ from column Department and also update the table.
mysql> Update Employee set Department = TRIM(Trailing 'Dept.' FROM Department); Query OK, 5 rows affected (0.10 sec) Rows matched: 5 Changed: 5 Warnings: 0 mysql> Select * from Employee; +------+----------------+------------+-----------------+ | Id | Name | Address | Department | +------+----------------+------------+-----------------+ | 100 | Raman | Delhi | IT | | 101 | Mohan | Haryana | History | | 102 | Shyam | Chandigarh | ENGLISH | | 103 | Sukhjeet Singh | Patiala | Computer Engg. | | 104 | Bimal Roy | Calcutta | Computer Engg. | +------+----------------+------------+-----------------+ 5 rows in set (0.00 sec)
The result set above shows that there is no prefix ‘Dept.’ with the column values.
- Related Articles
- A single MySQL query to update only specific records in a range without updating the entire column
- How to update MySQL column with random value?
- How do I remove ON UPDATE CURRENT_TIMESTAMP from an existing column in MySQL?
- How to remove all instances of a specific character from a column in MySQL?
- Update a column in MySQL and remove the trailing underscore values
- How to update a specific column value fetched with CASE statement?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- How to cast and update a numeric value from string column only where applicable in MySQL?
- Replace only a specific value from a column in MySQL
- Update an entire row in MySQL?
- MySQL insert a value to specific row and column
- Swap a specific column value in MySQL
- Update only a single column value in MySQL
- MySQL add “prefix” to every column?
- How to perform update in MySQL to disallow incrementing all the values above a specific value?

Advertisements