
- 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
Take off last character if a specific one exists in a string?
You can use trim() for this.Let us first create a table −
mysql> create table DemoTable ( UserId varchar(100) ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command. Here, we have added a question mark (?) to the end of some of the strings −
mysql> insert into DemoTable values('User123?'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('User777'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('User456'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('User133?'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+ | UserId | +----------+ | User123? | | User777 | | User456 | | User133? | +----------+ 4 rows in set (0.00 sec)
Following is the query to take off last character if a certain one exists in a string i.e.? in this case −
mysql> select trim(trailing '?' from UserId) from DemoTable;
This will produce the following output −
+--------------------------------+ | trim(trailing '?' from UserId) | +--------------------------------+ | User123 | | User777 | | User456 | | User133 | +--------------------------------+ 4 rows in set (0.04 sec)
- Related Articles
- Remove last char if it's a specific character in MySQL?
- Find last index of a character in a string in C++
- Finding the last occurrence of a character in a String in Java
- Add a character to a specific position in a string using Python
- Find if a substring exists within a string in Arduino
- How to remove the last character from a string in Java?
- Remove the last character from a string in the Swift language
- Getting last 5 character of a string with MySQL query?
- How to check if a string contains only one type of character in R?
- Find one extra character in a string using C++.
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- How to remove only last character from a string vector in R?
- How to check if a specific country code exists in a cell with MySQL?
- How to remove the first and last character in a string in R?
- Java Program to Replace the Spaces of a String with a Specific Character

Advertisements