
- 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
MySQL Query to remove all characters after last comma in string?
To remove all characters after the last comma in the string, you can use SUBSTRING_INDEX().
If you do not know the location of the last comma, then you need to find the last comma dynamically using LENGTH().
The syntax is as follows −
UPDATE yourTableName set yourColumnName = SUBSTRING_INDEX(yourColumnName, ',', LENGTH(yourColumnName) - LENGTH(REPLACE(yourColumnName, ',', '')));
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table RemoveAllCharacters -> ( -> Id int NOT NULL AUTO_INCREMENT, -> FullInfo varchar(200), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.83 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into RemoveAllCharacters(FullInfo) values('John,Smith,23,98,4565886'); Query OK, 1 row affected (0.23 sec) mysql> insert into RemoveAllCharacters(FullInfo) values('Carol,Taylor,26,91'); Query OK, 1 row affected (0.28 sec) mysql> insert into RemoveAllCharacters(FullInfo) values('David,Miller,21'); Query OK, 1 row affected (0.11 sec) mysql> insert into RemoveAllCharacters(FullInfo) values('Robert,Jones,22,97'); Query OK, 1 row affected (0.09 sec) mysql> insert into RemoveAllCharacters(FullInfo) values('Sam,Williams,27,88,456788,97877'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from RemoveAllCharacters;
The following is the output −
+----+---------------------------------+ | Id | FullInfo | +----+---------------------------------+ | 1 | John,Smith,23,98,4565886 | | 2 | Carol,Taylor,26,91 | | 3 | David,Miller,21 | | 4 | Robert,Jones,22,97 | | 5 | Sam,Williams,27,88,456788,97877 | +----+---------------------------------+ 5 rows in set (0.00 sec)
The following is the query to remove all characters after last comma in string −
mysql> update RemoveAllCharacters -> set FullInfo = SUBSTRING_INDEX(FullInfo, ',', LENGTH(FullInfo) - LENGTH(REPLACE(FullInfo, ',', ''))); Query OK, 5 rows affected (0.28 sec) Rows matched: 5 Changed: 5 Warnings: 0
Now check all the records of table once again. The query is as follows −
mysql> select *from RemoveAllCharacters;
The following is the output displaying the records without the characters after the last comma:
+----+---------------------------+ | Id | FullInfo | +----+---------------------------+ | 1 | John,Smith,23,98 | | 2 | Carol,Taylor,26 | | 3 | David,Miller | | 4 | Robert,Jones,22 | | 5 | Sam,Williams,27,88,456788 | +----+---------------------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL query to remove everything except the last 7 characters in column record
- How to remove all non-alphanumeric characters from a string in MySQL?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- MySQL query to get a substring from a string except the last three characters?
- MySQL query to replace a string after the last / in a column with directory links?
- MySQL query to split a column after specific characters?
- MySQL query to remove special characters from column values?
- MySQL query to find all rows where string contains less than four characters?
- Remove specific word in a comma separated string with MySQL
- MySQL query to search within the last 5 characters in a column?
- C++ Program to Remove all Characters in a String Except Alphabets
- MySQL query to sort by certain last string character?
- Remove first two characters of all fields in MySQL?
- MySQL query to get substrings (only the last three characters) from strings?
- Remove all non-alphabetical characters of a String in Java?
