
- 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 first two characters of all fields in MySQL?
To remove the first two characters of all fields, you need to use SUBSTRING() function from MySQL. The syntax is as follows −
UPDATE yourTableName SET yourColumnName=SUBSTRING(yourColumnName,3) WHERE yourCondition;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table RemoveFirstTwoCharacterDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> StringValue varchar(30), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.04 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into RemoveFirstTwoCharacterDemo(StringValue) values('U:100'); Query OK, 1 row affected (0.13 sec) mysql> insert into RemoveFirstTwoCharacterDemo(StringValue) values('S:20'); Query OK, 1 row affected (0.21 sec) mysql> insert into RemoveFirstTwoCharacterDemo(StringValue) values('N/A'); Query OK, 1 row affected (0.12 sec) mysql> insert into RemoveFirstTwoCharacterDemo(StringValue) values('T:8'); Query OK, 1 row affected (0.16 sec) mysql> insert into RemoveFirstTwoCharacterDemo(StringValue) values('N/A'); Query OK, 1 row affected (0.12 sec) mysql> insert into RemoveFirstTwoCharacterDemo(StringValue) values('W:99'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from RemoveFirstTwoCharacterDemo;
The following is the output −
+----+-------------+ | Id | StringValue | +----+-------------+ | 1 | U:100 | | 2 | S:20 | | 3 | N/A | | 4 | T:8 | | 5 | N/A | | 6 | W:99 | +----+-------------+ 6 rows in set (0.00 sec)
The following is the query to remove the first two characters −
mysql> update RemoveFirstTwoCharacterDemo set StringValue=SUBSTRING(StringValue,3) -> where StringValue <> 'N/A'; Query OK, 4 rows affected (0.27 sec) Rows matched: 4 Changed: 4 Warnings: 0
Check the table records once again using a select statement. The query is as follows −
mysql> select *from RemoveFirstTwoCharacterDemo;
The following is the output −
+----+-------------+ | Id | StringValue | +----+-------------+ | 1 | 100 | | 2 | 20 | | 3 | N/A | | 4 | 8 | | 5 | N/A | | 6 | 99 | +----+-------------+ 6 rows in set (0.00 sec)
- Related Articles
- Remove all characters of first string from second JavaScript
- Remove all except the first character of a string in MySQL?
- MySQL Query to remove all characters after last comma in string?
- How to remove all non-alphanumeric characters from a string in MySQL?
- How to select all the characters after the first 20 characters from a column in MySQL?
- Display all fields of a table in MySQL?
- JavaScript - Remove first n characters from string
- Remove all non-alphabetical characters of a String in Java?
- Remove new line characters from rows in MySQL?
- Remove specific fields/ rows and show other records in MySQL?
- Retrieve first 40 characters of a text field in MySQL?
- Get all characters before space in MySQL?
- Python - Remove all characters except letters and numbers
- How to count all characters in all rows of a field in MySQL?
- MySQL query to remove first digit?

Advertisements