
- 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
Remove last char if it's a specific character in MySQL?
To remove last char if it is a specific character then use SUBSTRING(). Let us first create a table −
mysql> create table DemoTable ( SubjectName varchar(100) ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('MongoDB?'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Java?'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-------------+ | SubjectName | +-------------+ | MySQL | | MongoDB? | | Java? | | C | +-------------+ 4 rows in set (0.00 sec)
Here is the query to remove last char if it's a specific character, for example question mark (?) here −
mysql> update DemoTable SET SubjectName = SUBSTRING(SubjectName, 1, CHAR_LENGTH(SubjectName) - 1) where SubjectName like '%?'; Query OK, 2 rows affected (0.25 sec) Rows matched : 2 Changed : 2 Warnings : 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
+-------------+ | SubjectName | +-------------+ | MySQL | | MongoDB | | Java | | C | +-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Take off last character if a specific one exists in a string?
- How to remove all instances of a specific character from a column in MySQL?
- How to change a specific char in a MySQL string?
- How to remove the last character from a string in Java?
- Remove the last character from a string in the Swift language
- Difference between char s[] and char *s in C
- How to remove only last character from a string vector in R?
- Fetch records containing a specific character twice in MySQL
- How to remove the first and last character in a string in R?
- Select all records if it contains specific number in MySQL?
- MySQL - Select all records if it contains specific number?
- Find records with a specific last digit in column with MySQL
- Getting last 5 character of a string with MySQL query?
- Remove specific word in a comma separated string with MySQL
- Select * but ignore displaying results containing a specific character in MySQL

Advertisements