- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 keep only first 2 characters in column value and delete rest of the characters?
To keep only first two characters and delete rest of the characters, use SUBSTRING().
Let us first create a table −
mysql> create table DemoTable743 (SubjectName varchar(100)); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable743 values('MySQL'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable743 values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable743 values('MongoDB'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable743 values('Python'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable743 values('Data Structure'); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable743 values('Algorithm'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable743;
This will produce the following output -
+----------------+ | SubjectName | +----------------+ | MySQL | | Java | | MongoDB | | Python | | Data Structure | | Algorithm | +----------------+ 6 rows in set (0.00 sec)
Following is the query to keep first 2 characters in column value and delete rest of the characters−
mysql> update DemoTable743 set SubjectName=SUBSTRING(SubjectName,1,2); Query OK, 6 rows affected (0.12 sec) Rows matched: 6 Changed: 6 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable743;
This will produce the following output -
+-------------+ | SubjectName | +-------------+ | My | | Ja | | Mo | | Py | | Da | | Al | +-------------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL query to retrieve only the column values with special characters?
- MySQL query to replace special characters from column value
- Return only the first 15 characters from a column with string values in MySQL
- How to select all the characters after the first 20 characters from a column in MySQL?
- MySQL query to split a column after specific characters?
- MySQL query to remove special characters from column values?
- MySQL query to search within the last 5 characters in a column?
- MySQL query to get substrings (only the last three characters) from strings?
- How to get first N characters from a MySQL column?
- Delete the first 10 characters from JTextArea in Java
- How to display only 200 characters from total value in MySQL?
- MySQL query to remove everything except the last 7 characters in column record
- MySQL query to delete last two words from every column value
- Append special characters to column values in MySQL
- Remove first two characters of all fields in MySQL?

Advertisements